We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6faf55e commit 5d15854Copy full SHA for 5d15854
C++/asteroid-collision.cpp
@@ -0,0 +1,26 @@
1
+// Time: O(n)
2
+// Space: O(n)
3
+
4
+class Solution {
5
+public:
6
+ vector<int> asteroidCollision(vector<int>& asteroids) {
7
+ vector<int> result;
8
+ for (const auto& asteroid : asteroids) {
9
+ bool is_exploded = false;
10
+ while (!result.empty() && asteroid < 0 && 0 < result.back()) {
11
+ if (result.back() < -asteroid) {
12
+ result.pop_back();
13
+ continue;
14
+ } else if (result.back() == -asteroid) {
15
16
+ }
17
+ is_exploded = true;
18
+ break;
19
20
+ if (!is_exploded) {
21
+ result.emplace_back(asteroid);
22
23
24
+ return result;
25
26
+};
0 commit comments