Back to DSA

Asteroid Collision

medium
Acceptance: 47%
StackArraySimulation

An array of integers represents a row of asteroids. The absolute value indicates size and the sign indicates direction: positive travels right, negative travels left. All asteroids move at identical speed. Simulate all collisions and return the final surviving asteroids. When two collide, the smaller one is destroyed; if they are the same size, both are destroyed. Asteroids headed the same way never collide.

Examples

Example 1:
Input:asteroids = [3,7,-4]
Output:[3,7]
Example 2:
Input:asteroids = [10,-10]
Output:[]

Hints

00:00
1234567