Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problems: No.2124,2126 #4133

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ tags:

<!-- solution:start -->

### 方法一:模拟
### 方法一:脑筋急转弯

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

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

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

<!-- tabs:start -->

Expand Down Expand Up @@ -97,7 +97,7 @@ class Solution {
class Solution {
public:
bool checkString(string s) {
return s.find("ba") == string::npos;
return !s.contains("ba");
}
};
```
Expand All @@ -110,6 +110,24 @@ func checkString(s string) bool {
}
```

#### TypeScript

```ts
function checkString(s: string): boolean {
return !s.includes('ba');
}
```

#### Rust

```rust
impl Solution {
pub fn check_string(s: String) -> bool {
!s.contains("ba")
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ There are no &#39;a&#39;s, hence, every &#39;a&#39; appears before every &#39;b&

<!-- solution:start -->

### Solution 1
### Solution 1: Brain Teaser

According to the problem statement, the string $s$ consists only of characters `a` and `b`.

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$.

The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -92,7 +98,7 @@ class Solution {
class Solution {
public:
bool checkString(string s) {
return s.find("ba") == string::npos;
return !s.contains("ba");
}
};
```
Expand All @@ -105,6 +111,24 @@ func checkString(s string) bool {
}
```

#### TypeScript

```ts
function checkString(s: string): boolean {
return !s.includes('ba');
}
```

#### Rust

```rust
impl Solution {
pub fn check_string(s: String) -> bool {
!s.contains("ba")
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Solution {
public:
bool checkString(string s) {
return s.find("ba") == string::npos;
return !s.contains("ba");
}
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
impl Solution {
pub fn check_string(s: String) -> bool {
!s.contains("ba")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function checkString(s: string): boolean {
return !s.includes('ba');
}
79 changes: 62 additions & 17 deletions solution/2100-2199/2126.Destroying Asteroids/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ tags:

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

根据题目描述,我们可以将小行星按质量从小到大排序,然后依次遍历小行星,如果行星的质量小于小行星的质量,那么行星将被摧毁,返回 `false`,否则行星将获得这颗小行星的质量。

如果所有小行星都能被摧毁,返回 `true`。

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是小行星的数量。

<!-- tabs:start -->

#### Python3
Expand All @@ -76,10 +82,10 @@ tags:
class Solution:
def asteroidsDestroyed(self, mass: int, asteroids: List[int]) -> bool:
asteroids.sort()
for v in asteroids:
if mass < v:
for x in asteroids:
if mass < x:
return False
mass += v
mass += x
return True
```

Expand All @@ -90,11 +96,11 @@ class Solution {
public boolean asteroidsDestroyed(int mass, int[] asteroids) {
Arrays.sort(asteroids);
long m = mass;
for (int v : asteroids) {
if (m < v) {
for (int x : asteroids) {
if (m < x) {
return false;
}
m += v;
m += x;
}
return true;
}
Expand All @@ -107,11 +113,13 @@ class Solution {
class Solution {
public:
bool asteroidsDestroyed(int mass, vector<int>& asteroids) {
sort(asteroids.begin(), asteroids.end());
ranges::sort(asteroids);
long long m = mass;
for (int v : asteroids) {
if (m < v) return false;
m += v;
for (int x : asteroids) {
if (m < x) {
return false;
}
m += x;
}
return true;
}
Expand All @@ -122,13 +130,12 @@ public:

```go
func asteroidsDestroyed(mass int, asteroids []int) bool {
m := mass
sort.Ints(asteroids)
for _, v := range asteroids {
if m < v {
for _, x := range asteroids {
if mass < x {
return false
}
m += v
mass += x
}
return true
}
Expand All @@ -139,16 +146,54 @@ func asteroidsDestroyed(mass int, asteroids []int) bool {
```ts
function asteroidsDestroyed(mass: number, asteroids: number[]): boolean {
asteroids.sort((a, b) => a - b);

for (const x of asteroids) {
if (mass < x) return false;
if (mass < x) {
return false;
}
mass += x;
}

return true;
}
```

#### Rust

```rust
impl Solution {
pub fn asteroids_destroyed(mass: i32, mut asteroids: Vec<i32>) -> bool {
let mut mass = mass as i64;
asteroids.sort_unstable();
for &x in &asteroids {
if mass < x as i64 {
return false;
}
mass += x as i64;
}
true
}
}
```

#### JavaScript

```js
/**
* @param {number} mass
* @param {number[]} asteroids
* @return {boolean}
*/
var asteroidsDestroyed = function (mass, asteroids) {
asteroids.sort((a, b) => a - b);
for (const x of asteroids) {
if (mass < x) {
return false;
}
mass += x;
}
return true;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
83 changes: 64 additions & 19 deletions solution/2100-2199/2126.Destroying Asteroids/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ All asteroids are destroyed.
<pre>
<strong>Input:</strong> mass = 5, asteroids = [4,9,23,4]
<strong>Output:</strong> false
<strong>Explanation:</strong>
<strong>Explanation:</strong>
The planet cannot ever gain enough mass to destroy the asteroid with a mass of 23.
After the planet destroys the other asteroids, it will have a mass of 5 + 4 + 9 + 4 = 22.
This is less than 23, so a collision would not destroy the last asteroid.</pre>
Expand All @@ -66,7 +66,13 @@ This is less than 23, so a collision would not destroy the last asteroid.</pre>

<!-- solution:start -->

### Solution 1
### Solution 1: Sorting + Greedy

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.

If all asteroids can be destroyed, return `true`.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Where $n$ is the number of asteroids.

<!-- tabs:start -->

Expand All @@ -76,10 +82,10 @@ This is less than 23, so a collision would not destroy the last asteroid.</pre>
class Solution:
def asteroidsDestroyed(self, mass: int, asteroids: List[int]) -> bool:
asteroids.sort()
for v in asteroids:
if mass < v:
for x in asteroids:
if mass < x:
return False
mass += v
mass += x
return True
```

Expand All @@ -90,11 +96,11 @@ class Solution {
public boolean asteroidsDestroyed(int mass, int[] asteroids) {
Arrays.sort(asteroids);
long m = mass;
for (int v : asteroids) {
if (m < v) {
for (int x : asteroids) {
if (m < x) {
return false;
}
m += v;
m += x;
}
return true;
}
Expand All @@ -107,11 +113,13 @@ class Solution {
class Solution {
public:
bool asteroidsDestroyed(int mass, vector<int>& asteroids) {
sort(asteroids.begin(), asteroids.end());
ranges::sort(asteroids);
long long m = mass;
for (int v : asteroids) {
if (m < v) return false;
m += v;
for (int x : asteroids) {
if (m < x) {
return false;
}
m += x;
}
return true;
}
Expand All @@ -122,13 +130,12 @@ public:

```go
func asteroidsDestroyed(mass int, asteroids []int) bool {
m := mass
sort.Ints(asteroids)
for _, v := range asteroids {
if m < v {
for _, x := range asteroids {
if mass < x {
return false
}
m += v
mass += x
}
return true
}
Expand All @@ -139,16 +146,54 @@ func asteroidsDestroyed(mass int, asteroids []int) bool {
```ts
function asteroidsDestroyed(mass: number, asteroids: number[]): boolean {
asteroids.sort((a, b) => a - b);

for (const x of asteroids) {
if (mass < x) return false;
if (mass < x) {
return false;
}
mass += x;
}

return true;
}
```

#### Rust

```rust
impl Solution {
pub fn asteroids_destroyed(mass: i32, mut asteroids: Vec<i32>) -> bool {
let mut mass = mass as i64;
asteroids.sort_unstable();
for &x in &asteroids {
if mass < x as i64 {
return false;
}
mass += x as i64;
}
true
}
}
```

#### JavaScript

```js
/**
* @param {number} mass
* @param {number[]} asteroids
* @return {boolean}
*/
var asteroidsDestroyed = function (mass, asteroids) {
asteroids.sort((a, b) => a - b);
for (const x of asteroids) {
if (mass < x) {
return false;
}
mass += x;
}
return true;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Loading
Loading