-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasteroidCollision.cpp
More file actions
32 lines (32 loc) · 932 Bytes
/
asteroidCollision.cpp
File metadata and controls
32 lines (32 loc) · 932 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
28
29
30
31
32
class Solution {
public:
vector<int> asteroidCollision(vector<int>& asteroids) {
stack <int> st;
for (int i=0; i<asteroids.size(); i++){
bool check = false;
while (!st.empty() && asteroids[i]<0 && st.top()>0){
if (abs(st.top()) < abs(asteroids[i])) {
st.pop();
}
else if (abs(st.top()) == abs(asteroids[i])) {
st.pop();
check = true;
break;
}
else {
check = true;
break;
}
}
if (!check) {
st.push(asteroids[i]);
}
}
vector<int> result(st.size());
for (int i = st.size() - 1; i >= 0; i--) {
result[i] = st.top();
st.pop();
}
return result;
}
};