LeetCode 735. Asteroid Collision All In One

xgqfrms發表於2024-05-31

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, 禁止轉載 🈲️,侵權必究⚠️!


相關文章