Asteroid Collision
MediumP2Coding
Asteroids move along a line. asteroids[i] gives the size of asteroid i by its absolute value and its direction by its sign: positive moves right, negative moves left. All 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 remain, in their original order, in the same signed form.
Example 1
- Input
- asteroids = [5,10,-5]
- Output
- [5,10]
- Why
- -5 hits 10 and explodes.
Example 2
- Input
- asteroids = [8,-8]
- Output
- []
Example 3
- Input
- asteroids = [10,2,-5]
- Output
- [10]
- Why
- -5 destroys 2, then 10 destroys -5.
Constraints
- 2 <= asteroids.length <= 10^4
- -1000 <= asteroids[i] <= 1000
- asteroids[i] != 0
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 12. ⌘Enter runs, ⌘⇧Enter submits.