
Stack
class Solution {
public int[] asteroidCollision(int[] asteroids) {
Stack<Integer> stack = new Stack<>();
for (int cur : asteroids) {
boolean alive = true;
while (!stack.isEmpty() && stack.peek() > 0 && cur < 0) {
if (stack.peek() < -cur) {
stack.pop();
alive = true;
} else if (stack.peek() == -cur) {
stack.pop();
alive = false;
break;
} else {
alive = false;
break;
}
}
if (alive) {
stack.push(cur);
}
}
int[] res = new int[stack.size()];
for (int i = res.length - 1; i >= 0; i--) {
res[i] = stack.pop();
}
return res;
}
}