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/0700-0799/0763.Partition Labels/README_EN.md
+15-1
Original file line number
Diff line number
Diff line change
@@ -58,7 +58,21 @@ A partition like "ababcbacadefegde", "hijhklij" is incorrect
58
58
59
59
<!-- solution:start -->
60
60
61
-
### Solution 1
61
+
### Solution 1: Greedy
62
+
63
+
We first use an array or hash table $\textit{last}$ to record the last occurrence of each letter in the string $s$.
64
+
65
+
Next, we use a greedy approach to partition the string into as many segments as possible.
66
+
67
+
Traverse the string $s$ from left to right, while maintaining the start index $j$ and end index $i$ of the current segment, both initially set to $0$.
68
+
69
+
For each letter $c$ visited, get the last occurrence position $\textit{last}[c]$. Since the end index of the current segment must not be less than $\textit{last}[c]$, let $\textit{mx} = \max(\textit{mx}, \textit{last}[c])$.
70
+
71
+
When visiting the index $\textit{mx}$, it means the current segment ends. The index range of the current segment is $[j,.. i]$, and the length is $i - j + 1$. We add this length to the result array. Then set $j = i + 1$ and continue to find the next segment.
72
+
73
+
Repeat the above process until the string traversal is complete to get the lengths of all segments.
74
+
75
+
Time complexity is $O(n)$, and space complexity is $O(|\Sigma|)$. Where $n$ is the length of the string $s$, and $|\Sigma|$ is the size of the character set. In this problem, $|\Sigma| = 26$.
Copy file name to clipboardexpand all lines: solution/0700-0799/0765.Couples Holding Hands/README_EN.md
+11-1
Original file line number
Diff line number
Diff line change
@@ -60,7 +60,17 @@ tags:
60
60
61
61
<!-- solution:start -->
62
62
63
-
### Solution 1
63
+
### Solution 1: Union-Find
64
+
65
+
We can assign a number to each pair of couples. Person with number $0$ and $1$ corresponds to couple $0$, person with number $2$ and $3$ corresponds to couple $1$, and so on. In other words, the person corresponding to $row[i]$ has a couple number of $\lfloor \frac{row[i]}{2} \rfloor$.
66
+
67
+
If there are $k$ pairs of couples who are seated incorrectly with respect to each other, i.e., if $k$ pairs of couples are in the same permutation cycle, it will take $k-1$ swaps for all of them to be seated correctly.
68
+
69
+
Why? Consider the following: we first adjust the positions of a couple to their correct seats. After this, the problem reduces from $k$ couples to $k-1$ couples. This process continues, and when $k = 1$, the number of swaps required is $0$. Therefore, if $k$ pairs of couples are in the wrong positions, we need $k-1$ swaps.
70
+
71
+
Thus, we only need to traverse the array once, use union-find to determine how many permutation cycles there are. Suppose there are $x$ cycles, and the size of each cycle (in terms of couple pairs) is $y_1, y_2, \cdots, y_x$. The number of swaps required is $y_1-1 + y_2-1 + \cdots + y_x-1 = y_1 + y_2 + \cdots + y_x - x = n - x$.
72
+
73
+
The time complexity is $O(n \times \alpha(n))$, and the space complexity is $O(n)$, where $\alpha(n)$ is the inverse Ackermann function, which can be considered a very small constant.
Copy file name to clipboardexpand all lines: solution/0700-0799/0768.Max Chunks To Make Sorted II/README_EN.md
+29-21
Original file line number
Diff line number
Diff line change
@@ -61,7 +61,11 @@ However, splitting into [2, 1], [3], [4], [4] is the highest number of chunks po
61
61
62
62
<!-- solution:start -->
63
63
64
-
### Solution 1
64
+
### Solution 1: Monotonic Stack
65
+
66
+
According to the problem, we can find that from left to right, each chunk has a maximum value, and these maximum values are monotonically increasing (non-strictly increasing). We can use a stack to store these maximum values of the chunks. The size of the final stack is the maximum number of chunks that can be sorted.
67
+
68
+
Time complexity is $O(n)$, where $n$ represents the length of $\textit{arr}$.
65
69
66
70
<!-- tabs:start -->
67
71
@@ -151,19 +155,19 @@ func maxChunksToSorted(arr []int) int {
151
155
152
156
```ts
153
157
function maxChunksToSorted(arr:number[]):number {
154
-
const stack = [];
155
-
for (const num ofarr) {
156
-
if (stack.length!==0&&num<stack[stack.length-1]) {
157
-
const max =stack.pop();
158
-
while (stack.length!==0&&num<stack[stack.length-1]) {
159
-
stack.pop();
160
-
}
161
-
stack.push(max);
158
+
const stk:number[] = [];
159
+
for (let v ofarr) {
160
+
if (stk.length===0||v>=stk[stk.length-1]) {
161
+
stk.push(v);
162
162
} else {
163
-
stack.push(num);
163
+
let mx =stk.pop()!;
164
+
while (stk.length>0&&stk[stk.length-1] >v) {
165
+
stk.pop();
166
+
}
167
+
stk.push(mx);
164
168
}
165
169
}
166
-
returnstack.length;
170
+
returnstk.length;
167
171
}
168
172
```
169
173
@@ -172,19 +176,23 @@ function maxChunksToSorted(arr: number[]): number {
0 commit comments