-
-
Notifications
You must be signed in to change notification settings - Fork 610
/
Copy pathAsteroidCollision.java
61 lines (58 loc) · 1.77 KB
/
AsteroidCollision.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package problems.medium;
import java.util.Arrays;
import java.util.Stack;
/**
* Why Did you create this class? what does it do?
*/
public class AsteroidCollision {
public static void main(String[] args) {
System.out.println(Arrays.toString(asteroidCollision(new int[] {
-2, -1, 1, 2
})));
System.out.println(Arrays.toString(asteroidCollision(new int[] {
5, 10, -5
})));
System.out.println(Arrays.toString(asteroidCollision(new int[] {
8, -8
})));
System.out.println(Arrays.toString(asteroidCollision(new int[] {
10, 2, -5
})));
System.out.println(Arrays.toString(asteroidCollision(new int[] {
10, 2, -5, -14
})));
}
static public int[] asteroidCollision(int[] a) {
if (a == null || a.length == 0)
return a;
Stack<Integer> st = new Stack<>();
for (int i = 0; i < a.length; i++) {
st.push(a[a.length - 1 - i]);
}
Stack<Integer> st2 = new Stack<>();
while (!st.isEmpty()) {
int pop = st.pop();
if (st2.isEmpty()) {
st2.add(pop);
continue;
}
if ((pop < 0 && st2.peek() > 0)) {
int pop2 = st2.pop();
if (Math.abs(pop2) > Math.abs(pop)) {
st2.add(pop2);
} else if (Math.abs(pop2) < Math.abs(pop)) {
st.add(pop);
}
} else {
st2.add(pop);
}
}
int[] res = new int[st2.size()];
int k = 0;
while (!st2.isEmpty()) {
res[res.length - 1 - k] = st2.pop();
k++;
}
return res;
}
}