Simulates asteroid collisions where larger survive, using a stack to process each asteroid in O(n).
## What is it?
Asteroids moving in a row collide when one moves right (+) and another moves left (−). Simulate the collisions — larger asteroids survive and smaller ones explode. Equal-sized asteroids both explode.
## How it works
- Use a stack representing surviving asteroids
- For each asteroid:
- If it moves right (+) → push it (no collision yet)
- If it moves left (−):
- While stack is not empty and the top is moving right (+) and is smaller than the current → pop (collision, top explodes)
- If stack top is equal in magnitude → pop and discard current (both explode)
- If stack top is larger → current is destroyed (discard)
- If stack is empty or top is also negative → push current
## When to use
- Simulation problems with "dominance" or collision mechanics
- Stack-based state machine problems
## Key Points
- O(n) time — each asteroid pushed and popped at most once
- O(n) space for the stack
- Careful handling of equal-magnitude collisions (both explode)