dsa · medium

Asteroid Collision

A row of asteroids moves left or right. Positive values move right, negative values move left, and each value is the asteroid's size. Two asteroids moving toward each other collide: the smaller one explodes, equal sizes explode together, and a larger one keeps going. Asteroids moving in the same direction never collide. Return the state of the row after all collisions.

Arguments

Example

asteroids = [5,10,-5]

10 and -5 move toward each other; 10 is larger, so -5 explodes and 10 survives → [5,10].

asteroids = [8,-8] → equal sizes explode together → [].

Constraints

2 <= asteroids.length <= 4*10^4 -1000 <= asteroids[i] <= 1000 asteroids[i] != 0 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.

Examples

Example 1

Input:
[5,10,-5]

Expected:
[5,10]

Example 2

Input:
[8,-8]

Expected:
[]

Example 3

Input:
[10,2,-5]

Expected:
[10]

Open in the Dojo editor