Asteroid Collision
Each integer in asteroids represents an asteroid on one line: its magnitude is its size, and its sign is its direction. Positive asteroids move right;…
Each integer in `asteroids` represents an asteroid on one line: its magnitude is its size, and its sign is its direction. Positive asteroids move right; negative asteroids move left. All move at the same speed. Only an earlier right-moving asteroid and a later left-moving asteroid can meet. The smaller explodes; equal magnitudes destroy both. A survivor can meet another asteroid, so continue until no collision is possible. Return the survivors in their original order; the result may be empty.
- `2 <= asteroids.length <= 100000` - `-1000000 <= asteroids[i] <= 1000000` - `asteroids[i] != 0` - Input order is position order; preserve the relative order of survivors.
Example 1: input asteroids = [1,2,3,-4,-2]; output [-4,-2]. The incoming -4 destroys 3, 2 and 1; the remaining -2 moves in the same direction.
Example 2: input asteroids = [5,10,-5,-10,8,-8,-3,12]; output [5,12]. Equal pairs disappear and the larger 5 stops -3; 12 remains behind 5.
Example 3: input asteroids = [5,-5]; output []. Equal magnitudes destroy both asteroids.
Category: Stack. Topic: stack. Difficulty: Medium. Patterns: stack.