The 30 second answer
Positive asteroids move right and negative ones move left, so a collision happens only when a right-moving one is immediately followed by a left-moving one. I push asteroids onto a stack, and while the new one is moving left and the top is moving right I resolve the fight: the smaller absolute value explodes, and equal sizes explode both. What remains on the stack is the final state, left to right.
Problem Each integer in the array is an asteroid: the absolute value is its size, and the sign is its direction, positive to the right and negative to the left, all at the same speed. When two meet, the smaller one explodes; equal sizes explode both. Same-direction asteroids never meet. Return the state after every collision. Original: LeetCode 735: Asteroid Collision.
Understanding the prompt The only colliding pair is a positive value immediately followed by a negative one, possibly after earlier collisions have been resolved. Leading left-movers and trailing right-movers never hit anything. The output should keep the surviving order.
Pattern recognition Stack matching, where the 'match' is a right-mover versus a later left-mover, and the comparison is on absolute size rather than bracket type.
Brute force Scan for an adjacent (+, −) pair, resolve it, and restart: O(n²) in the worst case.
Optimal Push each asteroid onto a stack. While the new asteroid is still alive, is negative, and the top is positive, compare sizes: if the top is smaller, pop and keep fighting; if they are equal, pop and the new one dies; if the top is larger, the new one dies. Push only if it survived. At the end, unroll the stack from bottom to top.
Alternatives A linked list of surviving asteroids is the same idea with different plumbing. The stack is the version to write in an interview.
Java solution
import java.util.ArrayDeque;
import java.util.Arrays;
public class AsteroidCollision {
static int[] asteroidCollision(int[] asteroids) {
var st = new ArrayDeque<Integer>();
for (int a : asteroids) {
boolean alive = true;
while (alive && a < 0 && !st.isEmpty() && st.peek() > 0) {
int top = st.peek();
if (top < -a) st.pop();
else {
if (top == -a) st.pop();
alive = false;
}
}
if (alive) st.push(a);
}
var ans = new int[st.size()];
for (int i = ans.length - 1; i >= 0; i--) ans[i] = st.pop();
return ans;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(asteroidCollision(new int[] {5, 10, -5}))); // [5, 10]
System.out.println(Arrays.toString(asteroidCollision(new int[] {8, -8}))); // []
System.out.println(Arrays.toString(asteroidCollision(new int[] {10, 2, -5}))); // [10]
System.out.println(Arrays.toString(asteroidCollision(new int[] {-2, -1, 1, 2}))); // [-2, -1, 1, 2]
}
}Complexity O(n) time, because each asteroid is pushed and popped at most once. O(n) extra space for the stack.
Edge cases
- [5,10,-5] becomes [5,10]: 10 destroys -5.
- [8,-8] becomes []: equal sizes destroy both.
- [10,2,-5] becomes [10]: -5 destroys 2, then 10 destroys -5.
- [-2,-1,1,2] stays the same: no right-then-left pair ever forms.
Follow-ups
- A variant with different speeds needs a different model; this problem relies on equal speed so only adjacent opposite signs collide.
Tests [5,10,-5] returns [5,10]. [8,-8] returns []. [10,2,-5] returns [10]. [-2,-1,1,2] returns [-2,-1,1,2].
How to say it in the interview "Collisions only happen when a right-moving asteroid is followed by a left-moving one. I push survivors onto a stack, and while that situation occurs I compare sizes and explode the smaller one, or both if they are equal. The stack from bottom to top is the final row. Linear time."
Sources (2)
- LeetCode 735: Asteroid Collisionleetcode.com
- Java SE 17 API: ArrayDequedocs.oracle.com