Skip to content

Commit 7fd03bc

Browse files
authoredFeb 24, 2025
feat: update lc problems (#4106)
1 parent ab9837a commit 7fd03bc

File tree

16 files changed

+196
-128
lines changed

16 files changed

+196
-128
lines changed
 

‎solution/0700-0799/0763.Partition Labels/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,19 @@ tags:
6060

6161
### 方法一:贪心
6262

63-
我们先用数组或哈希表 $last$ 记录字符串 $s$ 中每个字母最后一次出现的位置。
63+
我们先用数组或哈希表 $\textit{last}$ 记录字符串 $s$ 中每个字母最后一次出现的位置。
6464

6565
接下来我们使用贪心的方法,将字符串划分为尽可能多的片段。
6666

6767
从左到右遍历字符串 $s$,遍历的同时维护当前片段的开始下标 $j$ 和结束下标 $i$,初始均为 $0$。
6868

69-
对于每个访问到的字母 $c$,获取到最后一次出现的位置 $last[c]$。由于当前片段的结束下标一定不会小于 $last[c]$,因此令 $mx = \max(mx, last[c])$。
69+
对于每个访问到的字母 $c$,获取到最后一次出现的位置 $\textit{last}[c]$。由于当前片段的结束下标一定不会小于 $\textit{last}[c]$,因此令 $\textit{mx} = \max(\textit{mx}, \textit{last}[c])$。
7070

71-
当访问到下标 $mx$ 时,意味着当前片段访问结束,当前片段的下标范围是 $[j,.. i]$,长度为 $i - j + 1$,我们将其添加到结果数组中。然后令 $j = i + 1$, 继续寻找下一个片段。
71+
当访问到下标 $\textit{mx}$ 时,意味着当前片段访问结束,当前片段的下标范围是 $[j,.. i]$,长度为 $i - j + 1$,我们将其添加到结果数组中。然后令 $j = i + 1$, 继续寻找下一个片段。
7272

7373
重复上述过程,直至字符串遍历结束,即可得到所有片段的长度。
7474

75-
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串 $s$ 的长度,而 $C$ 为字符集的大小。本题中 $C = 26$。
75+
时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$。其中 $n$ 为字符串 $s$ 的长度,而 $|\Sigma|$ 为字符集的大小。本题中 $|\Sigma| = 26$。
7676

7777
<!-- tabs:start -->
7878

‎solution/0700-0799/0763.Partition Labels/README_EN.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,21 @@ A partition like &quot;ababcbacadefegde&quot;, &quot;hijhklij&quot; is incorrect
5858

5959
<!-- solution:start -->
6060

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

6377
<!-- tabs:start -->
6478

‎solution/0700-0799/0765.Couples Holding Hands/README_EN.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,17 @@ tags:
6060

6161
<!-- solution:start -->
6262

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

6575
<!-- tabs:start -->
6676

‎solution/0700-0799/0768.Max Chunks To Make Sorted II/README.md

+29-25
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ tags:
3333
<strong>输入:</strong>arr = [5,4,3,2,1]
3434
<strong>输出:</strong>1
3535
<strong>解释:</strong>
36-
将数组分成2块或者更多块,都无法得到所需的结果。
37-
例如,分成 [5, 4], [3, 2, 1] 的结果是 [4, 5, 1, 2, 3],这不是有序的数组。
36+
将数组分成2块或者更多块,都无法得到所需的结果。
37+
例如,分成 [5, 4], [3, 2, 1] 的结果是 [4, 5, 1, 2, 3],这不是有序的数组。
3838
</pre>
3939

4040
<p><strong class="example">示例 2:</strong></p>
@@ -43,8 +43,8 @@ tags:
4343
<strong>输入:</strong>arr = [2,1,3,4,4]
4444
<strong>输出:</strong>4
4545
<strong>解释:</strong>
46-
可以把它分成两块,例如 [2, 1], [3, 4, 4]。
47-
然而,分成 [2, 1], [3], [4], [4] 可以得到最多的块数。
46+
可以把它分成两块,例如 [2, 1], [3, 4, 4]。
47+
然而,分成 [2, 1], [3], [4], [4] 可以得到最多的块数。
4848
</pre>
4949

5050
<p>&nbsp;</p>
@@ -66,7 +66,7 @@ tags:
6666

6767
根据题目,我们可以发现,从左到右,每个分块都有一个最大值,并且这些分块的最大值呈单调递增(非严格递增)。我们可以用一个栈来存储这些分块的最大值。最后得到的栈的大小,也就是题目所求的最多能完成排序的块。
6868

69-
时间复杂度 $O(n)$,其中 $n$ 表示 $arr$ 的长度。
69+
时间复杂度 $O(n)$,其中 $n$ 表示 $\textit{arr}$ 的长度。
7070

7171
<!-- tabs:start -->
7272

@@ -156,19 +156,19 @@ func maxChunksToSorted(arr []int) int {
156156

157157
```ts
158158
function maxChunksToSorted(arr: number[]): number {
159-
const stack = [];
160-
for (const num of arr) {
161-
if (stack.length !== 0 && num < stack[stack.length - 1]) {
162-
const max = stack.pop();
163-
while (stack.length !== 0 && num < stack[stack.length - 1]) {
164-
stack.pop();
165-
}
166-
stack.push(max);
159+
const stk: number[] = [];
160+
for (let v of arr) {
161+
if (stk.length === 0 || v >= stk[stk.length - 1]) {
162+
stk.push(v);
167163
} else {
168-
stack.push(num);
164+
let mx = stk.pop()!;
165+
while (stk.length > 0 && stk[stk.length - 1] > v) {
166+
stk.pop();
167+
}
168+
stk.push(mx);
169169
}
170170
}
171-
return stack.length;
171+
return stk.length;
172172
}
173173
```
174174

@@ -177,19 +177,23 @@ function maxChunksToSorted(arr: number[]): number {
177177
```rust
178178
impl Solution {
179179
pub fn max_chunks_to_sorted(arr: Vec<i32>) -> i32 {
180-
let mut stack = vec![];
181-
for num in arr.iter() {
182-
if !stack.is_empty() && num < stack.last().unwrap() {
183-
let max = stack.pop().unwrap();
184-
while !stack.is_empty() && num < stack.last().unwrap() {
185-
stack.pop();
186-
}
187-
stack.push(max);
180+
let mut stk = Vec::new();
181+
for &v in arr.iter() {
182+
if stk.is_empty() || v >= *stk.last().unwrap() {
183+
stk.push(v);
188184
} else {
189-
stack.push(*num);
185+
let mut mx = stk.pop().unwrap();
186+
while let Some(&top) = stk.last() {
187+
if top > v {
188+
stk.pop();
189+
} else {
190+
break;
191+
}
192+
}
193+
stk.push(mx);
190194
}
191195
}
192-
stack.len() as i32
196+
stk.len() as i32
193197
}
194198
}
195199
```

‎solution/0700-0799/0768.Max Chunks To Make Sorted II/README_EN.md

+29-21
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ However, splitting into [2, 1], [3], [4], [4] is the highest number of chunks po
6161

6262
<!-- solution:start -->
6363

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

6670
<!-- tabs:start -->
6771

@@ -151,19 +155,19 @@ func maxChunksToSorted(arr []int) int {
151155

152156
```ts
153157
function maxChunksToSorted(arr: number[]): number {
154-
const stack = [];
155-
for (const num of arr) {
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 of arr) {
160+
if (stk.length === 0 || v >= stk[stk.length - 1]) {
161+
stk.push(v);
162162
} 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);
164168
}
165169
}
166-
return stack.length;
170+
return stk.length;
167171
}
168172
```
169173

@@ -172,19 +176,23 @@ function maxChunksToSorted(arr: number[]): number {
172176
```rust
173177
impl Solution {
174178
pub fn max_chunks_to_sorted(arr: Vec<i32>) -> i32 {
175-
let mut stack = vec![];
176-
for num in arr.iter() {
177-
if !stack.is_empty() && num < stack.last().unwrap() {
178-
let max = stack.pop().unwrap();
179-
while !stack.is_empty() && num < stack.last().unwrap() {
180-
stack.pop();
181-
}
182-
stack.push(max);
179+
let mut stk = Vec::new();
180+
for &v in arr.iter() {
181+
if stk.is_empty() || v >= *stk.last().unwrap() {
182+
stk.push(v);
183183
} else {
184-
stack.push(*num);
184+
let mut mx = stk.pop().unwrap();
185+
while let Some(&top) = stk.last() {
186+
if top > v {
187+
stk.pop();
188+
} else {
189+
break;
190+
}
191+
}
192+
stk.push(mx);
185193
}
186194
}
187-
stack.len() as i32
195+
stk.len() as i32
188196
}
189197
}
190198
```
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
impl Solution {
22
pub fn max_chunks_to_sorted(arr: Vec<i32>) -> i32 {
3-
let mut stack = vec![];
4-
for num in arr.iter() {
5-
if !stack.is_empty() && num < stack.last().unwrap() {
6-
let max = stack.pop().unwrap();
7-
while !stack.is_empty() && num < stack.last().unwrap() {
8-
stack.pop();
9-
}
10-
stack.push(max);
3+
let mut stk = Vec::new();
4+
for &v in arr.iter() {
5+
if stk.is_empty() || v >= *stk.last().unwrap() {
6+
stk.push(v);
117
} else {
12-
stack.push(*num);
8+
let mut mx = stk.pop().unwrap();
9+
while let Some(&top) = stk.last() {
10+
if top > v {
11+
stk.pop();
12+
} else {
13+
break;
14+
}
15+
}
16+
stk.push(mx);
1317
}
1418
}
15-
stack.len() as i32
19+
stk.len() as i32
1620
}
1721
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
function maxChunksToSorted(arr: number[]): number {
2-
const stack = [];
3-
for (const num of arr) {
4-
if (stack.length !== 0 && num < stack[stack.length - 1]) {
5-
const max = stack.pop();
6-
while (stack.length !== 0 && num < stack[stack.length - 1]) {
7-
stack.pop();
8-
}
9-
stack.push(max);
2+
const stk: number[] = [];
3+
for (let v of arr) {
4+
if (stk.length === 0 || v >= stk[stk.length - 1]) {
5+
stk.push(v);
106
} else {
11-
stack.push(num);
7+
let mx = stk.pop()!;
8+
while (stk.length > 0 && stk[stk.length - 1] > v) {
9+
stk.pop();
10+
}
11+
stk.push(mx);
1212
}
1313
}
14-
return stack.length;
14+
return stk.length;
1515
}

‎solution/0700-0799/0769.Max Chunks To Make Sorted/README.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ tags:
6868

6969
### 方法一:贪心 + 一次遍历
7070

71-
由于 $arr$ 是 $[0,..,n-1]$ 的一个排列,若已遍历过的数中的最大值 $mx$ 与当前遍历到的下标 $i$ 相等,说明可以进行一次分割,累加答案。
71+
由于 $\textit{arr}$ 是 $[0,..,n-1]$ 的一个排列,若已遍历过的数中的最大值 $\textit{mx}$ 与当前遍历到的下标 $i$ 相等,说明可以进行一次分割,累加答案。
7272

73-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $arr$ 的长度。
73+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $\textit{arr}$ 的长度。
7474

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

@@ -141,10 +141,10 @@ func maxChunksToSorted(arr []int) int {
141141
function maxChunksToSorted(arr: number[]): number {
142142
const n = arr.length;
143143
let ans = 0;
144-
let max = 0;
144+
let mx = 0;
145145
for (let i = 0; i < n; i++) {
146-
max = Math.max(arr[i], max);
147-
if (max == i) {
146+
mx = Math.max(arr[i], mx);
147+
if (mx == i) {
148148
ans++;
149149
}
150150
}
@@ -157,15 +157,15 @@ function maxChunksToSorted(arr: number[]): number {
157157
```rust
158158
impl Solution {
159159
pub fn max_chunks_to_sorted(arr: Vec<i32>) -> i32 {
160-
let mut res = 0;
161-
let mut max = 0;
160+
let mut ans = 0;
161+
let mut mx = 0;
162162
for i in 0..arr.len() {
163-
max = max.max(arr[i]);
164-
if max == (i as i32) {
165-
res += 1;
163+
mx = mx.max(arr[i]);
164+
if mx == (i as i32) {
165+
ans += 1;
166166
}
167167
}
168-
res
168+
ans
169169
}
170170
}
171171
```
@@ -176,15 +176,15 @@ impl Solution {
176176
#define max(a, b) (((a) > (b)) ? (a) : (b))
177177

178178
int maxChunksToSorted(int* arr, int arrSize) {
179-
int res = 0;
179+
int ans = 0;
180180
int mx = -1;
181181
for (int i = 0; i < arrSize; i++) {
182182
mx = max(mx, arr[i]);
183183
if (mx == i) {
184-
res++;
184+
ans++;
185185
}
186186
}
187-
return res;
187+
return ans;
188188
}
189189
```
190190
@@ -202,7 +202,7 @@ int maxChunksToSorted(int* arr, int arrSize) {
202202
203203
以上这种解法,不仅可以解决本题,也可以解决 [768. 最多能完成排序的块 II](https://github.com/doocs/leetcode/blob/main/solution/0700-0799/0768.Max%20Chunks%20To%20Make%20Sorted%20II/README.md) 这道困难题。大家可以自行尝试。
204204
205-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $arr$ 的长度。
205+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{arr}$ 的长度。
206206
207207
<!-- tabs:start -->
208208

0 commit comments

Comments
 (0)