xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

LeetCode 735. Asteroid Collision All In One

LeetCode 735. Asteroid Collision All In One

LeetCode 735. 小行星碰撞 算法题解

errors


/* 

Wrong Answer
241 / 275 testcases passed

Output
[-2]

Expected
[-2,-2,-2]

 */


// function asteroidCollision(asteroids: number[]): number[] {
//   // Asteroid Collision 小行星碰撞
//   if(asteroids.every(i => i > 0) || asteroids.every(i => i < 0)) {
//     return asteroids;
//   }
//   // 
//   let stack = [asteroids[0]];
//   for(let i = 1; i < asteroids.length; i++) {
//     stack.push(asteroids[i]);
//     console.log(`✅ stack =`, stack)
//     if((stack[stack.length - 2] > 0 && stack[stack.length - 1] < 0) || (stack[stack.length - 2] < 0 && stack[stack.length - 1] > 0)) {
//     console.log(`❌ stack =`, stack)
//       while(!stack.every(i => i > 0) || !stack.every(i => i < 0)) {
//         console.log(`stack.every(i => i > 0) =`, stack.every(i => i > 0))
//         console.log(`stack.every(i => i < 0) =`, stack.every(i => i < 0))
//         let top = stack[stack.length - 2];
//         let item = stack[stack.length - 1];
//         if(!top) {
//           break;
//         }
//         if(stack.every(i => i > 0)) {
//           break;
//         }
//         if(stack.every(i => i< 0)) {
//           break;
//         }
//         console.log(`top =`, top)
//         console.log(`item =`, item)
//         // asteroids[i] != 0 ✅
//         if(Math.abs(top) > Math.abs(item)) {
//           stack.pop();
//         }
//         // asteroids[i] === 0 ✅
//         if(Math.abs(top) === Math.abs(item)) {
//           stack.pop();
//           stack.pop();
//         }
//         if(Math.abs(top) < Math.abs(item)) {
//           stack.pop();
//           stack.pop();
//           stack.push(item)
//         }
//         console.log(`❓ stack =`, stack)
//       }
//     }
//   }
//   return stack;
// };



/* 

// 左右移动方向 bug ❌

Wrong Answer
143 / 275 testcases passed

Input
asteroids =
[-2,-1,1,2]

Output
[]
Expected
[-2,-1,1,2]

 */


// function asteroidCollision(asteroids: number[]): number[] {
//   // Asteroid Collision 小行星碰撞
//   let stack = [];
//   for(let i = 0; i < asteroids.length; i++) {
//     let item = asteroids[i];
//     if(!stack.length) {
//       stack.push(item);
//     } else {
//       while(!stack.every(i => i > 0) || !stack.every(i => i < 0)) {
//         let top = stack.pop();
//         if((top > 0 && item < 0) || (top < 0 && item > 0)) {
//           // asteroids[i] != 0 ✅
//           if(Math.abs(top) > Math.abs(item)) {
//             stack.push(top)
//           }
//           if(Math.abs(top) === Math.abs(item)) {
//             // do nothing
//           }
//           if(Math.abs(top) < Math.abs(item)) {
//             stack.push(item)
//           }
//         }
//         if((top > 0 && item > 0) || (top < 0 && item < 0)) {
//           stack.push(top);
//           stack.push(item);
//         }
//       }
//     }
//   }
//   return stack;
// };

// function asteroidCollision(asteroids: number[]): number[] {
//   // Asteroid Collision 小行星碰撞
//   let stack = [];
//   // let sign = true;
//   for(let i = 0; i < asteroids.length; i++) {
//     let item = asteroids[i];
//     while(stack.length) {
//       let top = stack.pop();
//       if(Math.abs(top) <= Math.abs(item) && item < 0) {
//         stack.push(item)
//       } else {
//         stack.push(top)
//       }
//       // if(sign && (top + asteroids[i]) < 0) {
//       //   stack.push(asteroids[i])
//       // } else {
//       //   stack.push(asteroids[i])
//       // }
//     }
//     // if(asteroids[i] < 0) {
//     //   sign = false;
//     // }
//     stack.push(asteroids[i])
//   }
// };

solutions

function asteroidCollision(asteroids: number[]): number[] {
  let stack = []
  for(let item of asteroids) {
    // move right ❌ move left
    while(stack.length && stack[stack.length - 1] > 0 && item < 0) {
      const top = stack.pop();
      if(top + item === 0) {
        // ignore
        item = 0
      } else {
        item = top + item > 0 ? top : item;
      }
    }
    if(item !== 0) {
      stack.push(item)
    }
  }
  return stack
};

image


function asteroidCollision(asteroids: number[]): number[] {
  // Asteroid Collision 小行星碰撞
  if(asteroids.every(i => i > 0) || asteroids.every(i => i < 0)) {
    return asteroids;
  }
  // 
  let stack = [asteroids[0]];
  for(let i = 1; i < asteroids.length; i++) {
    stack.push(asteroids[i]);
    // move right ❌ move left
    while(stack[stack.length - 2] && stack[stack.length - 2] > 0 && stack[stack.length - 1] < 0) {
      let second = stack[stack.length - 2];
      let top = stack[stack.length - 1];
      if(Math.abs(second) > Math.abs(top)) {
        // do nothing
        stack.pop();
      }
      if(Math.abs(second) === Math.abs(top)) {
        stack.pop();
        stack.pop();
      }
      if(Math.abs(second) < Math.abs(top)) {
        stack.pop();
        stack.pop();
        stack.push(top);
      }
    }
  }
  return stack;
};

demos


function asteroidCollision(asteroids: number[]): number[] {
  // Asteroid Collision 小行星碰撞
  if(asteroids.every(i => i > 0) || asteroids.every(i => i < 0)) {
    return asteroids;
  }
  let stack = [asteroids[0]];
  for(let i = 1; i < asteroids.length; i++) {
    stack.push(asteroids[i]);
    // move right ❌ move left
    while(stack[stack.length - 2] && stack[stack.length - 2] > 0 && stack[stack.length - 1] < 0) {
      let top = stack.pop();
      let second = stack.pop();;
      if(Math.abs(second) > Math.abs(top)) {
        stack.push(second);
      }
      if(Math.abs(second) === Math.abs(top)) {
        // do nothing
      }
      if(Math.abs(second) < Math.abs(top)) {
        stack.push(top);
      }
    }
  }
  return stack;
};

https://leetcode.com/problems/asteroid-collision/description/?envType=study-plan-v2&envId=leetcode-75

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

refs

https://leetcode.com/studyplan/leetcode-75/



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2024-05-31 02:42  xgqfrms  阅读(5)  评论(0编辑  收藏  举报