Skip to content

Commit 5a493f1

Browse files
committed
feat: add solutions to lc problem: No.0769
No.0769.Max Chunks To Make Sorted
1 parent c0b7c51 commit 5a493f1

File tree

7 files changed

+177
-18
lines changed

7 files changed

+177
-18
lines changed

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

+67-3
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,87 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
**方法一:一次遍历**
53+
54+
由于 $arr$ 是 $[0,..,n-1]$ 的一个排列,若已遍历过的数中的最大值 $mx$ 与当前遍历到的下标 $i$ 相等,说明可以进行一次分割,累加答案。
55+
56+
时间复杂度 $O(n)$,其中 $n$ 表示 $arr$ 的长度。
57+
5258
<!-- tabs:start -->
5359

5460
### **Python3**
5561

5662
<!-- 这里可写当前语言的特殊实现逻辑 -->
5763

5864
```python
59-
65+
class Solution:
66+
def maxChunksToSorted(self, arr: List[int]) -> int:
67+
mx = ans = 0
68+
for i, v in enumerate(arr):
69+
mx = max(mx, v)
70+
if i == mx:
71+
ans += 1
72+
return ans
6073
```
6174

6275
### **Java**
6376

6477
<!-- 这里可写当前语言的特殊实现逻辑 -->
6578

6679
```java
80+
class Solution {
81+
public int maxChunksToSorted(int[] arr) {
82+
int ans = 0;
83+
int mx = 0;
84+
for (int i = 0; i < arr.length; ++i) {
85+
mx = Math.max(mx, arr[i]);
86+
if (i == mx) {
87+
++ans;
88+
}
89+
}
90+
return ans;
91+
}
92+
}
93+
```
6794

95+
### **C++**
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
int maxChunksToSorted(vector<int>& arr) {
101+
int ans = 0;
102+
int mx = 0;
103+
for (int i = 0; i < arr.size(); ++i)
104+
{
105+
mx = max(mx, arr[i]);
106+
ans += i == mx;
107+
}
108+
return ans;
109+
}
110+
};
111+
```
112+
113+
### **Go**
114+
115+
```go
116+
func maxChunksToSorted(arr []int) int {
117+
ans, mx := 0, 0
118+
for i, v := range arr {
119+
mx = max(mx, v)
120+
if i == mx {
121+
ans++
122+
}
123+
}
124+
return ans
125+
}
126+
127+
func max(a, b int) int {
128+
if a > b {
129+
return a
130+
}
131+
return b
132+
}
68133
```
69134

70135
### **TypeScript**
@@ -75,8 +140,7 @@ function maxChunksToSorted(arr: number[]): number {
75140
let ans = 0;
76141
let max = 0;
77142
for (let i = 0; i < n; i++) {
78-
let cur = arr[i];
79-
max = Math.max(cur, max);
143+
max = Math.max(arr[i], max);
80144
if (max == i) {
81145
ans++;
82146
}

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

+61-3
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,72 @@ However, splitting into [1, 0], [2], [3], [4] is the highest number of chunks po
4848
### **Python3**
4949

5050
```python
51-
51+
class Solution:
52+
def maxChunksToSorted(self, arr: List[int]) -> int:
53+
mx = ans = 0
54+
for i, v in enumerate(arr):
55+
mx = max(mx, v)
56+
if i == mx:
57+
ans += 1
58+
return ans
5259
```
5360

5461
### **Java**
5562

5663
```java
64+
class Solution {
65+
public int maxChunksToSorted(int[] arr) {
66+
int ans = 0;
67+
int mx = 0;
68+
for (int i = 0; i < arr.length; ++i) {
69+
mx = Math.max(mx, arr[i]);
70+
if (i == mx) {
71+
++ans;
72+
}
73+
}
74+
return ans;
75+
}
76+
}
77+
```
78+
79+
### **C++**
80+
81+
```cpp
82+
class Solution {
83+
public:
84+
int maxChunksToSorted(vector<int>& arr) {
85+
int ans = 0;
86+
int mx = 0;
87+
for (int i = 0; i < arr.size(); ++i)
88+
{
89+
mx = max(mx, arr[i]);
90+
ans += i == mx;
91+
}
92+
return ans;
93+
}
94+
};
95+
```
5796
97+
### **Go**
98+
99+
```go
100+
func maxChunksToSorted(arr []int) int {
101+
ans, mx := 0, 0
102+
for i, v := range arr {
103+
mx = max(mx, v)
104+
if i == mx {
105+
ans++
106+
}
107+
}
108+
return ans
109+
}
110+
111+
func max(a, b int) int {
112+
if a > b {
113+
return a
114+
}
115+
return b
116+
}
58117
```
59118

60119
### **TypeScript**
@@ -65,8 +124,7 @@ function maxChunksToSorted(arr: number[]): number {
65124
let ans = 0;
66125
let max = 0;
67126
for (let i = 0; i < n; i++) {
68-
let cur = arr[i];
69-
max = Math.max(cur, max);
127+
max = Math.max(arr[i], max);
70128
if (max == i) {
71129
ans++;
72130
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int maxChunksToSorted(vector<int>& arr) {
4+
int ans = 0;
5+
int mx = 0;
6+
for (int i = 0; i < arr.size(); ++i)
7+
{
8+
mx = max(mx, arr[i]);
9+
ans += i == mx;
10+
}
11+
return ans;
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func maxChunksToSorted(arr []int) int {
2+
ans, mx := 0, 0
3+
for i, v := range arr {
4+
mx = max(mx, v)
5+
if i == mx {
6+
ans++
7+
}
8+
}
9+
return ans
10+
}
11+
12+
func max(a, b int) int {
13+
if a > b {
14+
return a
15+
}
16+
return b
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int maxChunksToSorted(int[] arr) {
3+
int ans = 0;
4+
int mx = 0;
5+
for (int i = 0; i < arr.length; ++i) {
6+
mx = Math.max(mx, arr[i]);
7+
if (i == mx) {
8+
++ans;
9+
}
10+
}
11+
return ans;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
class Solution:
2-
def maxChunksToSorted(self, arr):
3-
"""
4-
:type arr: List[int]
5-
:rtype: int
6-
"""
7-
ans = 0
8-
loc = 0
9-
for i, val in enumerate(arr):
10-
loc = val if loc < val else loc
11-
if loc == i:
2+
def maxChunksToSorted(self, arr: List[int]) -> int:
3+
mx = ans = 0
4+
for i, v in enumerate(arr):
5+
mx = max(mx, v)
6+
if i == mx:
127
ans += 1
138
return ans

solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ function maxChunksToSorted(arr: number[]): number {
33
let ans = 0;
44
let max = 0;
55
for (let i = 0; i < n; i++) {
6-
let cur = arr[i];
7-
max = Math.max(cur, max);
6+
max = Math.max(arr[i], max);
87
if (max == i) {
98
ans++;
109
}

0 commit comments

Comments
 (0)