-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAsteroidCollision.java
More file actions
58 lines (57 loc) · 2.08 KB
/
AsteroidCollision.java
File metadata and controls
58 lines (57 loc) · 2.08 KB
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
public class AsteroidCollision {
public static void main(String[] args) {
int[] asteroids={-1,1,2,-1};
System.out.println(Arrays.toString(asteroidCollision(asteroids)));
}
public static int[] asteroidCollision(int[] asteroids) {
Stack<Integer> st=new Stack<>();
List<Integer> ls=new ArrayList<>();
for(int i=0;i<asteroids.length;i++){
if(!st.isEmpty() && asteroids[i]<0 && st.peek()>0){
if(Math.abs(asteroids[i])==Math.abs(st.peek())){
st.pop();
ls.remove(ls.size()-1);
}
else{
boolean isBlast=false;
while(!st.isEmpty() && st.peek()>0 && asteroids[i]<0){
if(Math.abs(st.peek())<Math.abs(asteroids[i])){
st.pop();
ls.remove(ls.size()-1);
}
else if(Math.abs(st.peek())==Math.abs(asteroids[i])){
st.pop();
ls.remove(ls.size()-1);
isBlast=true;
break;
}
else{
break;
}
}
if(!isBlast && st.isEmpty()){
st.push(asteroids[i]);
ls.add(asteroids[i]);
}
else if(!isBlast && ((ls.get(ls.size()-1)<0 && asteroids[i]<0) || (ls.get(ls.size()-1)>0 && asteroids[i]>0))){
st.push(asteroids[i]);
ls.add(asteroids[i]);
}
}
}
else{
st.push(asteroids[i]);
ls.add(asteroids[i]);
}
}
int[] res=new int[ls.size()];
for(int i=0;i<res.length;i++){
res[i]=ls.get(i);
}
return res;
}
}