2
* @param {number[]} asteroids5
var asteroidCollision = function (asteroids) {7
for (let i = 0; i < asteroids.length; i++) {8
const a = asteroids[i];10
// Negative asteroids to the left of the stack can be ignored.11
// They'll never collide. Let's just add it to the answer stack and12
// move on. I consider this a special case.13
if ((s.length === 0 || s[s.length - 1] < 0) && a < 0) {16
// If an asteroid a is positive (l to r), it may still collide with an17
// a negative asteroid further on in the asteroids array21
// a is negative. It can only collide with positive ones in22
// the stack. The following will keep on iterating23
// until it is dealt with.27
// positive pop beats negative a, so pick up pop28
// and re-add it to the stack.29
if (Math.abs(pop) > Math.abs(a)) {32
// a has larger size than pop, so pop will get dropped33
// and we'll retry another iteration with the same34
// negative a asteroid and whatever the stack's state is.35
} else if (Math.abs(pop) < Math.abs(a)) {37
// magnitude of positive pop and negative a are the same38
// so we can drop both of them.45
// The stack should be the answer