Asteroid Collision
Asteroid Collision is LeetCode 735 (Medium). asteroids lists asteroids in a row. The sign of each value is its direction (positive moves right, negative moves left) and the absolute value is its size. All of them move at the same speed.
- When two asteroids meet, the smaller one explodes. If they are the same size, both explode.
- Two asteroids moving the same way never meet.
Return the asteroids that are left after every collision, in their original order.
- 2 <= asteroids.length <= 10⁴
- -1000 <= asteroids[i] <= 1000
- asteroids[i] != 0
Intuition
Only one arrangement can crash: a right-mover with a left-mover somewhere after it. A left-mover followed by a right-mover is flying apart.
Scan left to right and keep the survivors so far. A new right-mover cannot hit any kept asteroid yet. A new left-mover meets the kept right-movers starting with the newest one, because that one is closest to it. Newest first is exactly how a stack works.
So the left-mover pops smaller right-movers off the top until it explodes, meets one of equal size, or has no right-mover in front of it. In that last case nothing can ever reach it, so it stays.
Each new item either cancels the newest kept items or is kept itself, and the order of what is left matters: that is a stack scan, the same shape as Valid Parentheses or Removing Stars From a String.
Approach
Before reading on, decide which pairs of signs can ever meet. Then decide which kept asteroid a new left-mover hits first, and what happens after it wins. Aim for O(n) time.
Keep the survivors on a stack
Walk the array once. st holds the asteroids that have survived so far, in their original order, with the newest on top. A right-mover is pushed straight away: everything kept is behind it or flying away from it.
Let a left-mover fight the top
While the new asteroid a is negative and the top is positive, compare sizes:
- top smaller than
-a: pop it and keep fighting, because the next right-mover is now in front; - same size: pop it, and
aexplodes too; - top bigger:
aexplodes and the stack is left alone.
Push the left-mover if it survived
If a is still alive, the stack is empty or its top is another left-mover. Nothing behind it can ever catch it, so push it. When the scan ends, the stack from bottom to top is the answer, already in order.
Code in Python, C++ and Java
Common pitfalls
Comparing sizes without checking directions
while st and abs(st[-1]) < abs(a):
st.pop()while alive and a < 0 and st and st[-1] > 0:
Two left-movers never meet, but a size-only test lets the bigger one destroy the smaller. On [-1, -2] it returns [-2] instead of [-1, -2].
Fighting only once with if
if a < 0 and st and st[-1] > 0:
...while alive and a < 0 and st and st[-1] > 0:
...A big left-mover that wins keeps flying and hits the next right-mover too. On [1, 2, 3, -5] a single if returns [1, 2, -5]; the right answer is [-5].
Pushing after a same-size crash
elif st[-1] == -a:
st.pop()
break
...
st.append(a)elif st[-1] == -a:
st.pop()
alive = False
...
if alive:
st.append(a)Equal sizes destroy both. break leaves the loop but still reaches the push, so on [8, -8] the result is [-8] instead of []. The alive flag keeps "stop fighting" and "survived" apart.
Edge cases
For [-2, -1, 1, 2] every left-mover finds an empty stack or another left-mover on top, and every right-mover is pushed. Nothing meets, so the input comes back unchanged.
Complexity
Asteroid Collision FAQ
Does the gap between two asteroids matter?
No. All asteroids move at the same speed, so a right-mover and a left-mover after it close the gap at a steady rate and always meet, however far apart they start. That is why the array only needs to be read in order, with no positions or times.
Can it be done without an extra stack?
Yes. Use the input array itself as the stack: keep a write index top, overwrite asteroids[top] to push and decrease top to pop, then return the first top values. The extra space drops to O(1) besides the output, and the logic is unchanged.