Skip to content

Commit 80adc1c

Browse files
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 22.1 MB (39.97%)
1 parent f67f405 commit 80adc1c

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<p>We are given an array <code>asteroids</code> of integers representing asteroids in a row. The indices of the asteroid in the array represent their relative position in space.</p>
2+
3+
<p>For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.</p>
4+
5+
<p>Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> asteroids = [5,10,-5]
12+
<strong>Output:</strong> [5,10]
13+
<strong>Explanation:</strong> The 10 and -5 collide resulting in 10. The 5 and 10 never collide.
14+
</pre>
15+
16+
<p><strong class="example">Example 2:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> asteroids = [8,-8]
20+
<strong>Output:</strong> []
21+
<strong>Explanation:</strong> The 8 and -8 collide exploding each other.
22+
</pre>
23+
24+
<p><strong class="example">Example 3:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> asteroids = [10,2,-5]
28+
<strong>Output:</strong> [10]
29+
<strong>Explanation:</strong> The 2 and -5 collide resulting in -5. The 10 and -5 collide resulting in 10.
30+
</pre>
31+
32+
<p><strong class="example">Example 4:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong> asteroids = [3,5,-6,2,-1,4]​​​​​​​
36+
<strong>Output:</strong> [-6,2,4]
37+
<strong>Explanation:</strong> The asteroid -6 makes the asteroid 3 and 5 explode, and then continues going left. On the other side, the asteroid 2 makes the asteroid -1 explode and then continues going right, without reaching asteroid 4.
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>2 &lt;= asteroids.length &lt;= 10<sup>4</sup></code></li>
45+
<li><code>-1000 &lt;= asteroids[i] &lt;= 1000</code></li>
46+
<li><code>asteroids[i] != 0</code></li>
47+
</ul>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
vector<int> asteroidCollision(vector<int>& asteroids) {
4+
int n = asteroids.size();
5+
stack<int> stk;
6+
vector<int> res;
7+
for (int a : asteroids) {
8+
if (a > 0) stk.push(a);
9+
else {
10+
while (!stk.empty() && stk.top() < abs(a)) stk.pop();
11+
if (stk.empty()) res.push_back(a);
12+
if (!stk.empty() && abs(a) == stk.top()) stk.pop();
13+
}
14+
}
15+
vector<int> res2;
16+
while (!stk.empty()) {
17+
res2.push_back(stk.top());
18+
stk.pop();
19+
}
20+
int m = res2.size();
21+
for (int i=m-1; i>=0; i--) res.push_back(res2[i]);
22+
return res;
23+
}
24+
};

0 commit comments

Comments
 (0)