From 323ece71948e14ae8985ba35f499ada23de596b1 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 6 Mar 2025 17:44:02 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.2124,2126 --- .../README.md | 24 +++++- .../README_EN.md | 28 ++++++- .../Solution.cpp | 4 +- .../Solution.rs | 5 ++ .../Solution.ts | 3 + .../2126.Destroying Asteroids/README.md | 79 ++++++++++++++---- .../2126.Destroying Asteroids/README_EN.md | 83 ++++++++++++++----- .../2126.Destroying Asteroids/Solution.cpp | 12 +-- .../2126.Destroying Asteroids/Solution.go | 9 +- .../2126.Destroying Asteroids/Solution.java | 8 +- .../2126.Destroying Asteroids/Solution.js | 15 ++++ .../2126.Destroying Asteroids/Solution.py | 6 +- .../2126.Destroying Asteroids/Solution.rs | 13 +++ .../2126.Destroying Asteroids/Solution.ts | 6 +- 14 files changed, 232 insertions(+), 63 deletions(-) create mode 100644 solution/2100-2199/2124.Check if All A's Appears Before All B's/Solution.rs create mode 100644 solution/2100-2199/2124.Check if All A's Appears Before All B's/Solution.ts create mode 100644 solution/2100-2199/2126.Destroying Asteroids/Solution.js create mode 100644 solution/2100-2199/2126.Destroying Asteroids/Solution.rs diff --git a/solution/2100-2199/2124.Check if All A's Appears Before All B's/README.md b/solution/2100-2199/2124.Check if All A's Appears Before All B's/README.md index aa96d404a1291..c6c74ee04094a 100644 --- a/solution/2100-2199/2124.Check if All A's Appears Before All B's/README.md +++ b/solution/2100-2199/2124.Check if All A's Appears Before All B's/README.md @@ -63,13 +63,13 @@ tags: -### 方法一:模拟 +### 方法一:脑筋急转弯 根据题意,字符串 $s$ 仅由字符 `a`, `b` 组成。 要使得所有 `a` 都在 `b` 之前出现,需要满足 `b` 之后不会出现 `a`,也就是说,字符串 "ba" 不是字符串 $s$ 的子串,条件才能成立。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是字符串 $s$ 的长度。 +时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。 @@ -97,7 +97,7 @@ class Solution { class Solution { public: bool checkString(string s) { - return s.find("ba") == string::npos; + return !s.contains("ba"); } }; ``` @@ -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") + } +} +``` + diff --git a/solution/2100-2199/2124.Check if All A's Appears Before All B's/README_EN.md b/solution/2100-2199/2124.Check if All A's Appears Before All B's/README_EN.md index b0315271a3f92..42ba4c4a3fafb 100644 --- a/solution/2100-2199/2124.Check if All A's Appears Before All B's/README_EN.md +++ b/solution/2100-2199/2124.Check if All A's Appears Before All B's/README_EN.md @@ -64,7 +64,13 @@ There are no 'a's, hence, every 'a' appears before every 'b& -### 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)$. @@ -92,7 +98,7 @@ class Solution { class Solution { public: bool checkString(string s) { - return s.find("ba") == string::npos; + return !s.contains("ba"); } }; ``` @@ -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") + } +} +``` + diff --git a/solution/2100-2199/2124.Check if All A's Appears Before All B's/Solution.cpp b/solution/2100-2199/2124.Check if All A's Appears Before All B's/Solution.cpp index 6449d29a7c365..4cee382948f8f 100644 --- a/solution/2100-2199/2124.Check if All A's Appears Before All B's/Solution.cpp +++ b/solution/2100-2199/2124.Check if All A's Appears Before All B's/Solution.cpp @@ -1,6 +1,6 @@ class Solution { public: bool checkString(string s) { - return s.find("ba") == string::npos; + return !s.contains("ba"); } -}; \ No newline at end of file +}; diff --git a/solution/2100-2199/2124.Check if All A's Appears Before All B's/Solution.rs b/solution/2100-2199/2124.Check if All A's Appears Before All B's/Solution.rs new file mode 100644 index 0000000000000..c0e58baff25e8 --- /dev/null +++ b/solution/2100-2199/2124.Check if All A's Appears Before All B's/Solution.rs @@ -0,0 +1,5 @@ +impl Solution { + pub fn check_string(s: String) -> bool { + !s.contains("ba") + } +} diff --git a/solution/2100-2199/2124.Check if All A's Appears Before All B's/Solution.ts b/solution/2100-2199/2124.Check if All A's Appears Before All B's/Solution.ts new file mode 100644 index 0000000000000..c3b4c0506abaa --- /dev/null +++ b/solution/2100-2199/2124.Check if All A's Appears Before All B's/Solution.ts @@ -0,0 +1,3 @@ +function checkString(s: string): boolean { + return !s.includes('ba'); +} diff --git a/solution/2100-2199/2126.Destroying Asteroids/README.md b/solution/2100-2199/2126.Destroying Asteroids/README.md index 6b9676c7d44cb..3643563a50fcf 100644 --- a/solution/2100-2199/2126.Destroying Asteroids/README.md +++ b/solution/2100-2199/2126.Destroying Asteroids/README.md @@ -68,6 +68,12 @@ tags: ### 方法一:排序 + 贪心 +根据题目描述,我们可以将小行星按质量从小到大排序,然后依次遍历小行星,如果行星的质量小于小行星的质量,那么行星将被摧毁,返回 `false`,否则行星将获得这颗小行星的质量。 + +如果所有小行星都能被摧毁,返回 `true`。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是小行星的数量。 + #### Python3 @@ -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 ``` @@ -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; } @@ -107,11 +113,13 @@ class Solution { class Solution { public: bool asteroidsDestroyed(int mass, vector& 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; } @@ -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 } @@ -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) -> 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; +}; +``` + diff --git a/solution/2100-2199/2126.Destroying Asteroids/README_EN.md b/solution/2100-2199/2126.Destroying Asteroids/README_EN.md index 320661af7a0d3..8fee89bd64be9 100644 --- a/solution/2100-2199/2126.Destroying Asteroids/README_EN.md +++ b/solution/2100-2199/2126.Destroying Asteroids/README_EN.md @@ -46,7 +46,7 @@ All asteroids are destroyed.
 Input: mass = 5, asteroids = [4,9,23,4]
 Output: false
-Explanation: 
+Explanation:
 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.
@@ -66,7 +66,13 @@ This is less than 23, so a collision would not destroy the last asteroid. -### 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. @@ -76,10 +82,10 @@ This is less than 23, so a collision would not destroy the last asteroid. 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 ``` @@ -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; } @@ -107,11 +113,13 @@ class Solution { class Solution { public: bool asteroidsDestroyed(int mass, vector& 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; } @@ -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 } @@ -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) -> 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; +}; +``` + diff --git a/solution/2100-2199/2126.Destroying Asteroids/Solution.cpp b/solution/2100-2199/2126.Destroying Asteroids/Solution.cpp index 0b0b082a7a3dd..aa98e98bb82ea 100644 --- a/solution/2100-2199/2126.Destroying Asteroids/Solution.cpp +++ b/solution/2100-2199/2126.Destroying Asteroids/Solution.cpp @@ -1,12 +1,14 @@ class Solution { public: bool asteroidsDestroyed(int mass, vector& 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; } -}; \ No newline at end of file +}; diff --git a/solution/2100-2199/2126.Destroying Asteroids/Solution.go b/solution/2100-2199/2126.Destroying Asteroids/Solution.go index aa42e9942c176..1bce52b681bf1 100644 --- a/solution/2100-2199/2126.Destroying Asteroids/Solution.go +++ b/solution/2100-2199/2126.Destroying Asteroids/Solution.go @@ -1,11 +1,10 @@ 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 -} \ No newline at end of file +} diff --git a/solution/2100-2199/2126.Destroying Asteroids/Solution.java b/solution/2100-2199/2126.Destroying Asteroids/Solution.java index 796f4ce3b7a2e..3f08ac5feb7e3 100644 --- a/solution/2100-2199/2126.Destroying Asteroids/Solution.java +++ b/solution/2100-2199/2126.Destroying Asteroids/Solution.java @@ -2,12 +2,12 @@ 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; } -} \ No newline at end of file +} diff --git a/solution/2100-2199/2126.Destroying Asteroids/Solution.js b/solution/2100-2199/2126.Destroying Asteroids/Solution.js new file mode 100644 index 0000000000000..f606f8ce908f0 --- /dev/null +++ b/solution/2100-2199/2126.Destroying Asteroids/Solution.js @@ -0,0 +1,15 @@ +/** + * @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; +}; diff --git a/solution/2100-2199/2126.Destroying Asteroids/Solution.py b/solution/2100-2199/2126.Destroying Asteroids/Solution.py index 86c1d4956d8f8..1651bf79deef4 100644 --- a/solution/2100-2199/2126.Destroying Asteroids/Solution.py +++ b/solution/2100-2199/2126.Destroying Asteroids/Solution.py @@ -1,8 +1,8 @@ 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 diff --git a/solution/2100-2199/2126.Destroying Asteroids/Solution.rs b/solution/2100-2199/2126.Destroying Asteroids/Solution.rs new file mode 100644 index 0000000000000..5e4ad2f8f499c --- /dev/null +++ b/solution/2100-2199/2126.Destroying Asteroids/Solution.rs @@ -0,0 +1,13 @@ +impl Solution { + pub fn asteroids_destroyed(mass: i32, mut asteroids: Vec) -> 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 + } +} diff --git a/solution/2100-2199/2126.Destroying Asteroids/Solution.ts b/solution/2100-2199/2126.Destroying Asteroids/Solution.ts index 7039e052524c1..5daee021d0186 100644 --- a/solution/2100-2199/2126.Destroying Asteroids/Solution.ts +++ b/solution/2100-2199/2126.Destroying Asteroids/Solution.ts @@ -1,10 +1,10 @@ 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; }