Skip to content

Commit 9fa35bf

Browse files
committed
feat: leetcode 769
1 parent f38393e commit 9fa35bf

File tree

8 files changed

+113
-1
lines changed

8 files changed

+113
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<p align="center">
44
<!-- TOPICS COUNT START -->
5-
<img src="https://img.shields.io/badge/-进度:518-green" alt="进度:518">
5+
<img src="https://img.shields.io/badge/-进度:519-green" alt="进度:519">
66
<!-- TOPICS COUNT END -->
77
<a href="./assets/docs/TOPICS.md"><img src="https://img.shields.io/badge/-题库目录-blue" alt="题库目录"></a>
88
<a href="./assets/docs/CATEGORIES.md"><img src="https://img.shields.io/badge/-题库分类-red" alt="题库分类"></a>

assets/data/categories.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3682,6 +3682,12 @@
36823682
"path": "./problemset/set-intersection-size-at-least-two/README.md",
36833683
"difficulty": "困难"
36843684
},
3685+
{
3686+
"id": "769",
3687+
"title": "最多能完成排序的块",
3688+
"path": "./problemset/max-chunks-to-make-sorted/README.md",
3689+
"difficulty": "中等"
3690+
},
36853691
{
36863692
"id": "857",
36873693
"title": "雇佣 K 名工人的最低成本",

assets/data/topics.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3739,6 +3739,16 @@
37393739
"url": "https://leetcode.cn/problems/max-chunks-to-make-sorted-ii/",
37403740
"path": "./problemset/max-chunks-to-make-sorted-2/README.md"
37413741
},
3742+
{
3743+
"id": "769",
3744+
"title": {
3745+
"cn": "最多能完成排序的块",
3746+
"en": "max-chunks-to-make-sorted"
3747+
},
3748+
"difficulty": "中等",
3749+
"url": "https://leetcode.cn/problems/max-chunks-to-make-sorted/",
3750+
"path": "./problemset/max-chunks-to-make-sorted/README.md"
3751+
},
37423752
{
37433753
"id": "777",
37443754
"title": {

assets/docs/CATEGORIES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@
684684
| 646. [最长数对链](../../problemset/maximum-length-of-pair-chain/README.md) | 中等 |
685685
| 670. [最大交换](../../problemset/maximum-swap/README.md) | 中等 |
686686
| 757. [设置交集大小至少为2](../../problemset/set-intersection-size-at-least-two/README.md) | 困难 |
687+
| 769. [最多能完成排序的块](../../problemset/max-chunks-to-make-sorted/README.md) | 中等 |
687688
| 857. [雇佣 K 名工人的最低成本](../../problemset/minimum-cost-to-hire-k-workers/README.md) | 困难 |
688689
| 870. [优势洗牌](../../problemset/advantage-shuffle/README.md) | 中等 |
689690
| 871. [最低加油次数](../../problemset/minimum-number-of-refueling-stops/README.md) | 困难 |

assets/docs/TOPICS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,8 @@
748748

749749
[768. 最多能完成排序的块 II](../../problemset/max-chunks-to-make-sorted-2/README.md)
750750

751+
[769. 最多能完成排序的块](../../problemset/max-chunks-to-make-sorted/README.md)
752+
751753
[777. 在LR字符串中交换相邻字符](../../problemset/swap-adjacent-in-lr-string/README.md)
752754

753755
[780. 到达终点](../../problemset/reaching-points/README.md)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# 最多能完成排序的块
2+
3+
> 难度:中等
4+
>
5+
> https://leetcode.cn/problems/max-chunks-to-make-sorted/
6+
7+
## 题目
8+
9+
给定一个长度为 `n` 的整数数组 `arr` ,它表示在 `[0, n - 1]` 范围内的整数的排列。
10+
11+
我们将 `arr` **分割成若干** 块 (即分区),并对每个块单独排序。将它们连接起来后,使得连接的结果和按升序排序后的原数组相同。
12+
13+
返回数组能分成的最多块数量。
14+
15+
### 示例
16+
17+
#### 示例 1:
18+
19+
```
20+
输入: arr = [4,3,2,1,0]
21+
输出: 1
22+
解释:
23+
将数组分成2块或者更多块,都无法得到所需的结果。
24+
例如,分成 [4, 3], [2, 1, 0] 的结果是 [3, 4, 0, 1, 2],这不是有序的数组。
25+
```
26+
27+
#### 示例 2:
28+
29+
```
30+
输入: arr = [1,0,2,3,4]
31+
输出: 4
32+
解释:
33+
我们可以把它分成两块,例如 [1, 0], [2, 3, 4]。
34+
然而,分成 [1, 0], [2], [3], [4] 可以得到最多的块数。
35+
```
36+
37+
## 解题
38+
39+
```ts
40+
/**
41+
* 贪心算法
42+
* @desc 时间复杂度 O(N) 空间复杂度 O(1)
43+
* @param arr
44+
* @returns
45+
*/
46+
export function maxChunksToSorted(arr: number[]): number {
47+
let m = 0
48+
let res = 0
49+
for (let i = 0; i < arr.length; i++) {
50+
m = Math.max(m, arr[i])
51+
if (m === i)
52+
res++
53+
}
54+
return res
55+
}
56+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { maxChunksToSorted } from '.'
3+
4+
describe('最多能完成排序的块', () => {
5+
testCase(maxChunksToSorted)
6+
})
7+
8+
function testCase(fn: (arr: number[]) => number) {
9+
it.each([
10+
[
11+
[4, 3, 2, 1, 0],
12+
1,
13+
],
14+
[
15+
[1, 0, 2, 3, 4],
16+
4,
17+
],
18+
])('示例%#', (arr, expected) => {
19+
expect(fn(arr)).toBe(expected)
20+
})
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* 贪心算法
3+
* @desc 时间复杂度 O(N) 空间复杂度 O(1)
4+
* @param arr
5+
* @returns
6+
*/
7+
export function maxChunksToSorted(arr: number[]): number {
8+
let m = 0
9+
let res = 0
10+
for (let i = 0; i < arr.length; i++) {
11+
m = Math.max(m, arr[i])
12+
if (m === i)
13+
res++
14+
}
15+
return res
16+
}

0 commit comments

Comments
 (0)