Asteroid Collision
Given an array asteroids where each value's absolute size is its mass and its sign is its direction (positive means moving right, negative means moving left), simulate their collisions. All asteroids move at the same speed. Two asteroids collide only when a right-moving one is followed by a left-moving one; the smaller explodes, and if they are equal both explode. Return the state of the asteroids after all collisions.
Open official problem prompt ↗Determine which asteroids remain after all direction-based collisions resolve.
Like cars merging onto a one-lane road: only a car going right that is immediately in front of an oncoming left car can crash, and the lighter one is wrecked.
- Input
- asteroids = [5, 10, -5]
- Output
- [5, 10]
- Why
- The -5 meets the right-moving 10; since 10 > 5, the -5 explodes and 5 and 10 (both never colliding, since 5 is left of 10 and same direction) survive.
2 <= asteroids.length <= 10^4-1000 <= asteroids[i] <= 1000asteroids[i] != 0