Solution to leetcode 735
https://leetcode.com/problems/asteroid-collision/
class Solution {
public int[] asteroidCollision(int[] asteroids) {
Stack<Integer> stack = new Stack();
for(int i = 0; i < asteroids.length; i++){
int cur = asteroids[i];
boolean push = false;
//only when previous is positive and cur is negative will the asteriods collide
while(!stack.isEmpty() && stack.peek() > 0 && cur < 0){
int prev = stack.pop();
if(prev + cur < 0){
continue;
}
else if(prev + cur > 0){
stack.add(prev);
push = true;
break;
}
else{
push = true;
break;
}
}
if(push == false){
stack.add(cur);
}
}
int n = stack.size();
int[] output = new int[n];
n = n - 1;
while(n >= 0){
output[n] = stack.pop();
n--;
}
return output;
}
}