Skip to content

Commit c6eb8a9

Browse files
authored
Create destroying-asteroids.cpp
1 parent 0245bf0 commit c6eb8a9

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

C++/destroying-asteroids.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Time: O(nlogn)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
bool asteroidsDestroyed(int mass, vector<int>& asteroids) {
7+
sort(begin(asteroids), end(asteroids));
8+
for (const auto& x : asteroids) {
9+
if (x > mass) {
10+
return false;
11+
}
12+
mass += min(x, asteroids.back() - mass);
13+
}
14+
return true;
15+
}
16+
};

0 commit comments

Comments
 (0)