Skip to content

Commit 056f365

Browse files
committed
Runtime: 22 ms (Top 6.85%) | Memory: 49 MB (Top 11.86%)
1 parent 96808c0 commit 056f365

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Runtime: 22 ms (Top 6.85%) | Memory: 49 MB (Top 11.86%)
2+
/*
3+
0. Start iterating over the asteroid one by one.
4+
1. If the stack is not empty, and its top > 0 (right moving asteroid) and current asteroid < 0 (left moving), we have collision.
5+
2. Pop all the smaller sized right moving asteroids (i.e. values > 0 but lesser than absolute value of left moving asteroid i.e. abs(<0))
6+
3. Now that we have taken care of collisions with smaller size right moving asteroids, we need to see if there's a same sized right moving asteroid. If yes, just remove that one as well. Do not add the current left moving asteroid to the stack as it will be annihilated by the same sized right moving asteroid. Continue to the next iteration, we are done handling with this left moving asteroid.
7+
4. If we are here, we still need to deal with the current left moving asteroid. Check the top of the stack as to what is there on top. If its a larger sized right moving asteroid, it will annihilate this current left moving asteroid. So Continue to the next iteration, we are done handling with this left moving asteroid.
8+
5. If we are here, it means the current asteroid has survived till now either because it did not meet any collisions or won in the collisions. In this case, push the asteroid on to the stack.
9+
6. Convert the stack to an array in return it.
10+
11+
*/
12+
class Solution {
13+
public int[] asteroidCollision(int[] asteroids) {
14+
Stack<Integer> stack = new Stack<>();
15+
//0. Start iterating over the asteroid one by one.
16+
for(int a : asteroids) {
17+
18+
//1. If the stack is not empty, and its top > 0 (right moving asteroid) and current asteroid < 0 (left moving), we have collision.
19+
//2. Pop all the smaller sized right moving asteroids (i.e. values > 0 but lesser than absolute value of left moving asteroid i.e. abs(<0))
20+
while(!stack.isEmpty() && stack.peek() > 0 && a < 0 && stack.peek() < Math.abs(a)) {
21+
stack.pop();
22+
}
23+
24+
//3. Now that we have taken care of collisions with smaller size right moving asteroids, we need to see if there's a same sized right moving asteroid. If yes, just remove that one as well. Do not add the current left moving asteroid to the stack as it will be annihilated by the same sized right moving asteroid. Continue to the next iteration, we are done handling with this left moving asteroid.
25+
if(!stack.isEmpty() && stack.peek() > 0 && a < 0 && stack.peek() == Math.abs(a)) {
26+
stack.pop();
27+
continue;
28+
}
29+
30+
//4. If we are here, we still need to deal with the current left moving asteroid. Check the top of the stack as to what is there on top. If its a larger sized right moving asteroid, it will annihilate this current left moving asteroid. So Continue to the next iteration, we are done handling with this left moving asteroid.
31+
if(!stack.isEmpty() && stack.peek() > 0 && a < 0 && stack.peek() > Math.abs(a)) {
32+
continue;
33+
}
34+
35+
//5. If we are here, it means the current asteroid has survived till now either because it did not meet any collisions or won in the collisions. In this case, push the asteroid on to the stack.
36+
stack.push(a);
37+
38+
}
39+
40+
//6. Convert the stack to an array in return it.
41+
int[] ans = new int[stack.size()];
42+
int i = stack.size() - 1;
43+
while(!stack.isEmpty()) {
44+
ans[i] = stack.pop();
45+
i--;
46+
}
47+
return ans;
48+
}
49+
}

0 commit comments

Comments
 (0)