-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathastroidCollision.java
More file actions
27 lines (26 loc) · 832 Bytes
/
astroidCollision.java
File metadata and controls
27 lines (26 loc) · 832 Bytes
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
import java.util.Stack;
public class astroidCollision {
public static int[] asteroidCollision(int[] asteroids) {
Stack<Integer> ast=new Stack<>();
for (int a : asteroids) {
if (ast.isEmpty()||a>0) {
ast.push(a);
} else {
while(true) {
if (ast.peek()<0&&a<0) {
ast.push(a);
break;
} else if (ast.peek()>0&&a<0) {
int p=ast.pop();
ast.push((Math.abs(a)>Math.abs(p))?a:p);
break;
} else if (ast.peek()==-a) {
ast.pop();
break;
}
}
}
}
return new int[]{};
}
}