Skip to content

Commit b415dfd

Browse files
committed
feat: add solutions to lc problem: No.2126
No.2126.Destroying Asteroids
1 parent 806a5a7 commit b415dfd

File tree

6 files changed

+155
-2
lines changed

6 files changed

+155
-2
lines changed

solution/2100-2199/2126.Destroying Asteroids/README.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,77 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:排序 + 贪心**
54+
5355
<!-- tabs:start -->
5456

5557
### **Python3**
5658

5759
<!-- 这里可写当前语言的特殊实现逻辑 -->
5860

5961
```python
60-
62+
class Solution:
63+
def asteroidsDestroyed(self, mass: int, asteroids: List[int]) -> bool:
64+
asteroids.sort()
65+
for v in asteroids:
66+
if mass < v:
67+
return False
68+
mass += v
69+
return True
6170
```
6271

6372
### **Java**
6473

6574
<!-- 这里可写当前语言的特殊实现逻辑 -->
6675

6776
```java
77+
class Solution {
78+
public boolean asteroidsDestroyed(int mass, int[] asteroids) {
79+
Arrays.sort(asteroids);
80+
long m = mass;
81+
for (int v : asteroids) {
82+
if (m < v) {
83+
return false;
84+
}
85+
m += v;
86+
}
87+
return true;
88+
}
89+
}
90+
```
91+
92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
bool asteroidsDestroyed(int mass, vector<int>& asteroids) {
98+
sort(asteroids.begin(), asteroids.end());
99+
long long m = mass;
100+
for (int v : asteroids)
101+
{
102+
if (m < v) return false;
103+
m += v;
104+
}
105+
return true;
106+
}
107+
};
108+
```
68109
110+
### **Go**
111+
112+
```go
113+
func asteroidsDestroyed(mass int, asteroids []int) bool {
114+
m := mass
115+
sort.Ints(asteroids)
116+
for _, v := range asteroids {
117+
if m < v {
118+
return false
119+
}
120+
m += v
121+
}
122+
return true
123+
}
69124
```
70125

71126
### **TypeScript**

solution/2100-2199/2126.Destroying Asteroids/README_EN.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,66 @@ This is less than 23, so a collision would not destroy the last asteroid.</pre>
5151
### **Python3**
5252

5353
```python
54-
54+
class Solution:
55+
def asteroidsDestroyed(self, mass: int, asteroids: List[int]) -> bool:
56+
asteroids.sort()
57+
for v in asteroids:
58+
if mass < v:
59+
return False
60+
mass += v
61+
return True
5562
```
5663

5764
### **Java**
5865

5966
```java
67+
class Solution {
68+
public boolean asteroidsDestroyed(int mass, int[] asteroids) {
69+
Arrays.sort(asteroids);
70+
long m = mass;
71+
for (int v : asteroids) {
72+
if (m < v) {
73+
return false;
74+
}
75+
m += v;
76+
}
77+
return true;
78+
}
79+
}
80+
```
81+
82+
### **C++**
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
bool asteroidsDestroyed(int mass, vector<int>& asteroids) {
88+
sort(asteroids.begin(), asteroids.end());
89+
long long m = mass;
90+
for (int v : asteroids)
91+
{
92+
if (m < v) return false;
93+
m += v;
94+
}
95+
return true;
96+
}
97+
};
98+
```
6099
100+
### **Go**
101+
102+
```go
103+
func asteroidsDestroyed(mass int, asteroids []int) bool {
104+
m := mass
105+
sort.Ints(asteroids)
106+
for _, v := range asteroids {
107+
if m < v {
108+
return false
109+
}
110+
m += v
111+
}
112+
return true
113+
}
61114
```
62115

63116
### **TypeScript**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
bool asteroidsDestroyed(int mass, vector<int>& asteroids) {
4+
sort(asteroids.begin(), asteroids.end());
5+
long long m = mass;
6+
for (int v : asteroids)
7+
{
8+
if (m < v) return false;
9+
m += v;
10+
}
11+
return true;
12+
}
13+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func asteroidsDestroyed(mass int, asteroids []int) bool {
2+
m := mass
3+
sort.Ints(asteroids)
4+
for _, v := range asteroids {
5+
if m < v {
6+
return false
7+
}
8+
m += v
9+
}
10+
return true
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public boolean asteroidsDestroyed(int mass, int[] asteroids) {
3+
Arrays.sort(asteroids);
4+
long m = mass;
5+
for (int v : asteroids) {
6+
if (m < v) {
7+
return false;
8+
}
9+
m += v;
10+
}
11+
return true;
12+
}
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def asteroidsDestroyed(self, mass: int, asteroids: List[int]) -> bool:
3+
asteroids.sort()
4+
for v in asteroids:
5+
if mass < v:
6+
return False
7+
mass += v
8+
return True

0 commit comments

Comments
 (0)