Asteroid Collision
MediumStack
Decode it first. Clues, constraints, pattern and plan before any code.A row of asteroids is given as a list of non-zero integers. The absolute value is an asteroid's size, and the sign is its direction: positive moves right, negative moves left. All asteroids move at the same speed.
When two asteroids meet, the smaller one explodes. If they are the same size, both explode. Two asteroids moving in the same direction never meet. Return the asteroids that are left after all collisions, in order.
[5, 10, -5] → [5, 10] (-5 hits 10 and explodes)
[8, -8] → [] (same size, both explode)
[10, 2, -5] → [10] (-5 destroys 2, then 10 destroys -5)
[-2, -1, 1, 2] → [-2, -1, 1, 2] (nobody meets)Constraints
2 <= asteroids.length <= 10^4-1000 <= asteroids[i] <= 1000asteroids[i] != 0