Skip to content

Commit c99e4d6

Browse files
authored
feat: add solutions to lc problems: No.2124,2126 (#4133)
1 parent 2e67a4c commit c99e4d6

File tree

14 files changed

+232
-63
lines changed

14 files changed

+232
-63
lines changed

solution/2100-2199/2124.Check if All A's Appears Before All B's/README.md

+21-3
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ tags:
6363

6464
<!-- solution:start -->
6565

66-
### 方法一:模拟
66+
### 方法一:脑筋急转弯
6767

6868
根据题意,字符串 $s$ 仅由字符 `a`, `b` 组成。
6969

7070
要使得所有 `a` 都在 `b` 之前出现,需要满足 `b` 之后不会出现 `a`,也就是说,字符串 "ba" 不是字符串 $s$ 的子串,条件才能成立。
7171

72-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是字符串 $s$ 的长度。
72+
时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$
7373

7474
<!-- tabs:start -->
7575

@@ -97,7 +97,7 @@ class Solution {
9797
class Solution {
9898
public:
9999
bool checkString(string s) {
100-
return s.find("ba") == string::npos;
100+
return !s.contains("ba");
101101
}
102102
};
103103
```
@@ -110,6 +110,24 @@ func checkString(s string) bool {
110110
}
111111
```
112112

113+
#### TypeScript
114+
115+
```ts
116+
function checkString(s: string): boolean {
117+
return !s.includes('ba');
118+
}
119+
```
120+
121+
#### Rust
122+
123+
```rust
124+
impl Solution {
125+
pub fn check_string(s: String) -> bool {
126+
!s.contains("ba")
127+
}
128+
}
129+
```
130+
113131
<!-- tabs:end -->
114132

115133
<!-- solution:end -->

solution/2100-2199/2124.Check if All A's Appears Before All B's/README_EN.md

+26-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ There are no &#39;a&#39;s, hence, every &#39;a&#39; appears before every &#39;b&
6464

6565
<!-- solution:start -->
6666

67-
### Solution 1
67+
### Solution 1: Brain Teaser
68+
69+
According to the problem statement, the string $s$ consists only of characters `a` and `b`.
70+
71+
To ensure that all `a`s appear before all `b`s, the condition that must be met is that `b` should not appear before `a`. In other words, the substring "ba" should not be present in the string $s$.
72+
73+
The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$.
6874

6975
<!-- tabs:start -->
7076

@@ -92,7 +98,7 @@ class Solution {
9298
class Solution {
9399
public:
94100
bool checkString(string s) {
95-
return s.find("ba") == string::npos;
101+
return !s.contains("ba");
96102
}
97103
};
98104
```
@@ -105,6 +111,24 @@ func checkString(s string) bool {
105111
}
106112
```
107113

114+
#### TypeScript
115+
116+
```ts
117+
function checkString(s: string): boolean {
118+
return !s.includes('ba');
119+
}
120+
```
121+
122+
#### Rust
123+
124+
```rust
125+
impl Solution {
126+
pub fn check_string(s: String) -> bool {
127+
!s.contains("ba")
128+
}
129+
}
130+
```
131+
108132
<!-- tabs:end -->
109133

110134
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution {
22
public:
33
bool checkString(string s) {
4-
return s.find("ba") == string::npos;
4+
return !s.contains("ba");
55
}
6-
};
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
impl Solution {
2+
pub fn check_string(s: String) -> bool {
3+
!s.contains("ba")
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function checkString(s: string): boolean {
2+
return !s.includes('ba');
3+
}

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

+62-17
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ tags:
6868

6969
### 方法一:排序 + 贪心
7070

71+
根据题目描述,我们可以将小行星按质量从小到大排序,然后依次遍历小行星,如果行星的质量小于小行星的质量,那么行星将被摧毁,返回 `false`,否则行星将获得这颗小行星的质量。
72+
73+
如果所有小行星都能被摧毁,返回 `true`
74+
75+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是小行星的数量。
76+
7177
<!-- tabs:start -->
7278

7379
#### Python3
@@ -76,10 +82,10 @@ tags:
7682
class Solution:
7783
def asteroidsDestroyed(self, mass: int, asteroids: List[int]) -> bool:
7884
asteroids.sort()
79-
for v in asteroids:
80-
if mass < v:
85+
for x in asteroids:
86+
if mass < x:
8187
return False
82-
mass += v
88+
mass += x
8389
return True
8490
```
8591

@@ -90,11 +96,11 @@ class Solution {
9096
public boolean asteroidsDestroyed(int mass, int[] asteroids) {
9197
Arrays.sort(asteroids);
9298
long m = mass;
93-
for (int v : asteroids) {
94-
if (m < v) {
99+
for (int x : asteroids) {
100+
if (m < x) {
95101
return false;
96102
}
97-
m += v;
103+
m += x;
98104
}
99105
return true;
100106
}
@@ -107,11 +113,13 @@ class Solution {
107113
class Solution {
108114
public:
109115
bool asteroidsDestroyed(int mass, vector<int>& asteroids) {
110-
sort(asteroids.begin(), asteroids.end());
116+
ranges::sort(asteroids);
111117
long long m = mass;
112-
for (int v : asteroids) {
113-
if (m < v) return false;
114-
m += v;
118+
for (int x : asteroids) {
119+
if (m < x) {
120+
return false;
121+
}
122+
m += x;
115123
}
116124
return true;
117125
}
@@ -122,13 +130,12 @@ public:
122130
123131
```go
124132
func asteroidsDestroyed(mass int, asteroids []int) bool {
125-
m := mass
126133
sort.Ints(asteroids)
127-
for _, v := range asteroids {
128-
if m < v {
134+
for _, x := range asteroids {
135+
if mass < x {
129136
return false
130137
}
131-
m += v
138+
mass += x
132139
}
133140
return true
134141
}
@@ -139,16 +146,54 @@ func asteroidsDestroyed(mass int, asteroids []int) bool {
139146
```ts
140147
function asteroidsDestroyed(mass: number, asteroids: number[]): boolean {
141148
asteroids.sort((a, b) => a - b);
142-
143149
for (const x of asteroids) {
144-
if (mass < x) return false;
150+
if (mass < x) {
151+
return false;
152+
}
145153
mass += x;
146154
}
147-
148155
return true;
149156
}
150157
```
151158

159+
#### Rust
160+
161+
```rust
162+
impl Solution {
163+
pub fn asteroids_destroyed(mass: i32, mut asteroids: Vec<i32>) -> bool {
164+
let mut mass = mass as i64;
165+
asteroids.sort_unstable();
166+
for &x in &asteroids {
167+
if mass < x as i64 {
168+
return false;
169+
}
170+
mass += x as i64;
171+
}
172+
true
173+
}
174+
}
175+
```
176+
177+
#### JavaScript
178+
179+
```js
180+
/**
181+
* @param {number} mass
182+
* @param {number[]} asteroids
183+
* @return {boolean}
184+
*/
185+
var asteroidsDestroyed = function (mass, asteroids) {
186+
asteroids.sort((a, b) => a - b);
187+
for (const x of asteroids) {
188+
if (mass < x) {
189+
return false;
190+
}
191+
mass += x;
192+
}
193+
return true;
194+
};
195+
```
196+
152197
<!-- tabs:end -->
153198

154199
<!-- solution:end -->

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

+64-19
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ All asteroids are destroyed.
4646
<pre>
4747
<strong>Input:</strong> mass = 5, asteroids = [4,9,23,4]
4848
<strong>Output:</strong> false
49-
<strong>Explanation:</strong>
49+
<strong>Explanation:</strong>
5050
The planet cannot ever gain enough mass to destroy the asteroid with a mass of 23.
5151
After the planet destroys the other asteroids, it will have a mass of 5 + 4 + 9 + 4 = 22.
5252
This is less than 23, so a collision would not destroy the last asteroid.</pre>
@@ -66,7 +66,13 @@ This is less than 23, so a collision would not destroy the last asteroid.</pre>
6666

6767
<!-- solution:start -->
6868

69-
### Solution 1
69+
### Solution 1: Sorting + Greedy
70+
71+
According to the problem description, we can sort the asteroids by mass in ascending order, and then iterate through the asteroids. If the planet's mass is less than the asteroid's mass, the planet will be destroyed, and we return `false`. Otherwise, the planet will gain the mass of the asteroid.
72+
73+
If all asteroids can be destroyed, return `true`.
74+
75+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Where $n$ is the number of asteroids.
7076

7177
<!-- tabs:start -->
7278

@@ -76,10 +82,10 @@ This is less than 23, so a collision would not destroy the last asteroid.</pre>
7682
class Solution:
7783
def asteroidsDestroyed(self, mass: int, asteroids: List[int]) -> bool:
7884
asteroids.sort()
79-
for v in asteroids:
80-
if mass < v:
85+
for x in asteroids:
86+
if mass < x:
8187
return False
82-
mass += v
88+
mass += x
8389
return True
8490
```
8591

@@ -90,11 +96,11 @@ class Solution {
9096
public boolean asteroidsDestroyed(int mass, int[] asteroids) {
9197
Arrays.sort(asteroids);
9298
long m = mass;
93-
for (int v : asteroids) {
94-
if (m < v) {
99+
for (int x : asteroids) {
100+
if (m < x) {
95101
return false;
96102
}
97-
m += v;
103+
m += x;
98104
}
99105
return true;
100106
}
@@ -107,11 +113,13 @@ class Solution {
107113
class Solution {
108114
public:
109115
bool asteroidsDestroyed(int mass, vector<int>& asteroids) {
110-
sort(asteroids.begin(), asteroids.end());
116+
ranges::sort(asteroids);
111117
long long m = mass;
112-
for (int v : asteroids) {
113-
if (m < v) return false;
114-
m += v;
118+
for (int x : asteroids) {
119+
if (m < x) {
120+
return false;
121+
}
122+
m += x;
115123
}
116124
return true;
117125
}
@@ -122,13 +130,12 @@ public:
122130
123131
```go
124132
func asteroidsDestroyed(mass int, asteroids []int) bool {
125-
m := mass
126133
sort.Ints(asteroids)
127-
for _, v := range asteroids {
128-
if m < v {
134+
for _, x := range asteroids {
135+
if mass < x {
129136
return false
130137
}
131-
m += v
138+
mass += x
132139
}
133140
return true
134141
}
@@ -139,16 +146,54 @@ func asteroidsDestroyed(mass int, asteroids []int) bool {
139146
```ts
140147
function asteroidsDestroyed(mass: number, asteroids: number[]): boolean {
141148
asteroids.sort((a, b) => a - b);
142-
143149
for (const x of asteroids) {
144-
if (mass < x) return false;
150+
if (mass < x) {
151+
return false;
152+
}
145153
mass += x;
146154
}
147-
148155
return true;
149156
}
150157
```
151158

159+
#### Rust
160+
161+
```rust
162+
impl Solution {
163+
pub fn asteroids_destroyed(mass: i32, mut asteroids: Vec<i32>) -> bool {
164+
let mut mass = mass as i64;
165+
asteroids.sort_unstable();
166+
for &x in &asteroids {
167+
if mass < x as i64 {
168+
return false;
169+
}
170+
mass += x as i64;
171+
}
172+
true
173+
}
174+
}
175+
```
176+
177+
#### JavaScript
178+
179+
```js
180+
/**
181+
* @param {number} mass
182+
* @param {number[]} asteroids
183+
* @return {boolean}
184+
*/
185+
var asteroidsDestroyed = function (mass, asteroids) {
186+
asteroids.sort((a, b) => a - b);
187+
for (const x of asteroids) {
188+
if (mass < x) {
189+
return false;
190+
}
191+
mass += x;
192+
}
193+
return true;
194+
};
195+
```
196+
152197
<!-- tabs:end -->
153198

154199
<!-- solution:end -->

0 commit comments

Comments
 (0)