You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2100-2199/2124.Check if All A's Appears Before All B's/README_EN.md
+26-2
Original file line number
Diff line number
Diff line change
@@ -64,7 +64,13 @@ There are no 'a's, hence, every 'a' appears before every 'b&
64
64
65
65
<!-- solution:start -->
66
66
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)$.
Copy file name to clipboardexpand all lines: solution/2100-2199/2126.Destroying Asteroids/README_EN.md
+64-19
Original file line number
Diff line number
Diff line change
@@ -46,7 +46,7 @@ All asteroids are destroyed.
46
46
<pre>
47
47
<strong>Input:</strong> mass = 5, asteroids = [4,9,23,4]
48
48
<strong>Output:</strong> false
49
-
<strong>Explanation:</strong>
49
+
<strong>Explanation:</strong>
50
50
The planet cannot ever gain enough mass to destroy the asteroid with a mass of 23.
51
51
After the planet destroys the other asteroids, it will have a mass of 5 + 4 + 9 + 4 = 22.
52
52
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>
66
66
67
67
<!-- solution:start -->
68
68
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.
70
76
71
77
<!-- tabs:start -->
72
78
@@ -76,10 +82,10 @@ This is less than 23, so a collision would not destroy the last asteroid.</pre>
0 commit comments