The trick here is to name the state correctly, then let the implementation follow. For 735. Asteroid Collision, the solution in this repository is mainly a stack solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: stack.
The notes already sitting in the source point us in the right direction:
- stack
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are asteroidCollision.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- The stack stores unfinished context, which is usually the cleanest way to handle nested or monotonic structure.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(N), space: O(N)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//stack
02//Runtime: 1072 ms, faster than 5.51% of C++ online submissions for Asteroid Collision.
03//Memory Usage: 21 MB, less than 95.21% of C++ online submissions for Asteroid Collision.
04class Solution {
05public:
06 vector<int> asteroidCollision(vector<int>& asteroids) {
07 stack<int> stk;
08
09 bool changed = false;
10
11 do{
12 changed = false;
13 for(int& cur : asteroids){
14 if(!stk.empty() && stk.top() > 0 && cur < 0){
15 int prev = stk.top();
16 if(abs(prev) > abs(cur)){
17 //do nothing
18 }else if(abs(prev) == abs(cur)){
19 stk.pop();
20 }else{
21 //abs(prev) < abs(cur)
22 stk.pop();
23 stk.push(cur);
24 }
25 }else{
26 stk.push(cur);
27 }
28 }
29
30 if(stk.size() < asteroids.size()){
31 changed = true;
32 }
33
34 asteroids.clear();
35
36 while(!stk.empty()){
37 asteroids.insert(asteroids.begin(), stk.top());
38 stk.pop();
39 }
40 }while(changed);
41
42 return asteroids;
43 }
44};
45
46//official sol
47//stack, keep going left until exploded
48//Runtime: 20 ms, faster than 95.41% of C++ online submissions for Asteroid Collision.
49//Memory Usage: 18.3 MB, less than 95.21% of C++ online submissions for Asteroid Collision.
50//time: O(N), space: O(N)
51class Solution {
52public:
53 vector<int> asteroidCollision(vector<int>& asteroids) {
54 stack<int> stk;
55
56 for(int& cur : asteroids){
57 bool exploded = false;
58 while(!stk.empty() && stk.top() > 0 && cur < 0){
59 int prev = stk.top();
60 if(abs(prev) > abs(cur)){
61 //do nothing
62 exploded = true;
63 break;
64 }else if(abs(prev) == abs(cur)){
65 stk.pop();
66 exploded = true;
67 break;
68 }else{
69 //abs(prev) < abs(cur)
70 stk.pop();
71 //cur keeps going left
72 }
73 }
74
75 if(!exploded){
76 stk.push(cur);
77 }
78 }
79
80 asteroids.clear();
81
82 while(!stk.empty()){
83 asteroids.insert(asteroids.begin(), stk.top());
84 stk.pop();
85 }
86
87 return asteroids;
88 }
89};
90
91//two pointer
92//https://leetcode.com/problems/asteroid-collision/solution/131673
93//Runtime: 24 ms, faster than 81.17% of C++ online submissions for Asteroid Collision.
94//Memory Usage: 17.9 MB, less than 95.21% of C++ online submissions for Asteroid Collision.
95class Solution {
96public:
97 vector<int> asteroidCollision(vector<int>& A) {
98 int n = A.size();
99
100 //slow: last filled asteroid's position
101 int slow = -1, fast = 0;
102
103 for(fast = 0; fast < n; ++fast){
104 bool exploded = false;
105
106 while(slow >= 0 && A[slow] > 0 && A[fast] < 0){
107 if(abs(A[slow]) > abs(A[fast])){
108 exploded = true;
109 break;
110 }else if(abs(A[slow]) == abs(A[fast])){
111 --slow;
112 exploded = true;
113 break;
114 }else{
115 --slow;
116 }
117 }
118
119 if(!exploded){
120 A[++slow] = A[fast];
121 }
122 }
123
124 return vector<int>(A.begin(), A.begin()+slow+1);
125 }
126};
Cost