package leetcode;

import java.util.Arrays;
import java.util.Stack;

/**
 * https://leetcode.com/problems/asteroid-collision/
 */
public class Problem735 {
    public int[] asteroidCollision(int[] asteroids) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < asteroids.length; i++) {
            if (stack.isEmpty()) {
                stack.push(asteroids[i]);
            } else {
                int a = stack.pop();
                if (a > 0 && asteroids[i] < 0) {
                    int sum = a + asteroids[i];
                    if (sum < 0) {
                        stack.push(asteroids[i]);
                    } else if (sum > 0) {
                        stack.push(a);
                    }
                }
            }
        }
        int[] result = new int[stack.size()];
        for (int i = stack.size() - 1; i >= 0; i--) {
            result[i] = stack.pop();
        }
        return result;
    }

    public static void main(String[] args) {
        Problem735 prob = new Problem735();
        System.out.println(Arrays.toString(prob.asteroidCollision(new int[]{5, 10, -5}))); // [5, 10]
        System.out.println(Arrays.toString(prob.asteroidCollision(new int[]{8, -8}))); // []
        System.out.println(Arrays.toString(prob.asteroidCollision(new int[]{10, 2, -5}))); // [10]
        System.out.println(Arrays.toString(prob.asteroidCollision(new int[]{-2, -1, 1, 2}))); // [-2, -1, 1, 2]
    }
}

@@ -16,33 +16,8 @@
 e;%0A%0A
-import java.util.Arrays;%0A
 impo
@@ -293,71 +293,204 @@
     
-if (stack.isEmpty()) %7B%0A                stack.push(asteroids%5Bi%5D)
+stack.push(asteroids%5Bi%5D);%0A            if (stack.size() %3C= 1) %7B%0A                continue;%0A            %7D%0A            while (true) %7B%0A                if (stack.size() %3C= 1) %7B%0A                    break
 ;%0A  
@@ -503,16 +503,13 @@
     
-%7D else %7B
+    %7D
 %0A   
@@ -525,17 +525,61 @@
     int 
-a
+right = stack.pop();%0A                int left
  = stack
@@ -610,17 +610,20 @@
 if (
-a
+left
  %3E 0 && 
 aste
@@ -618,28 +618,21 @@
  %3E 0 && 
-asteroids%5Bi%5D
+right
  %3C 0) %7B%0A
@@ -665,24 +665,20 @@
 m = 
-a + asteroids%5Bi%5D
+left + right
 ;%0A  
@@ -741,36 +741,29 @@
  stack.push(
-asteroids%5Bi%5D
+right
 );%0A         
@@ -830,17 +830,20 @@
 ck.push(
-a
+left
 );%0A     
@@ -876,30 +876,23 @@
        %7D
-%0A            %7D
+ else %7B
 %0A       
@@ -892,26 +892,24 @@
         
-%7D%0A
         
 int%5B%5D re
@@ -904,145 +904,108 @@
     
-int%5B%5D result = new int%5Bstack.size()%5D;%0A        for (int i = stack.size() - 1; i %3E= 0; i--) %7B%0A            result%5Bi%5D = stack.pop();%0A
+    stack.push(left);%0A                    stack.push(right);%0A                    break;%0A    
         
 %7D%0A  
@@ -992,32 +992,36 @@
 ak;%0A            
+    
 %7D%0A        return
@@ -1018,117 +1018,68 @@
     
-return result;%0A
     %7D%0A
-%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem735 prob = new Problem735
+    %7D%0A        int%5B%5D result = new int%5Bstack.size
 ()
+%5D
 ;%0A  
@@ -1088,405 +1088,123 @@
     
-System.out.println(Arrays.toString(prob.asteroidCollision(new int%5B%5D%7B5, 10, -5%7D))); // %5B5, 10%5D%0A        System.out.println(Arrays.toString(prob.asteroidCollision(new int%5B%5D%7B8, -8%7D))); // %5B%5D%0A        System.out.println(Arrays.toString(prob.asteroidCollision(new int%5B%5D%7B10, 2, -5%7D))); // %5B10%5D%0A        System.out.println(Arrays.toString(prob.asteroidCollision(new int%5B%5D%7B-2, -1, 1, 2%7D))); // %5B-2, -1, 1, 2%5D
+for (int i = stack.size() - 1; i %3E= 0; i--) %7B%0A            result%5Bi%5D = stack.pop();%0A        %7D%0A        return result;
 %0A   

Solve problem 735