Skip to content

Commit 1a6245d

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

File tree

8 files changed

+215
-4
lines changed

8 files changed

+215
-4
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
- [每日温度](/solution/0700-0799/0739.Daily%20Temperatures/README.md) - `单调栈`
6060
- [子数组的最小值之和](/solution/0900-0999/0907.Sum%20of%20Subarray%20Minimums/README.md) - `单调栈`
6161
- [最大宽度坡](/solution/0900-0999/0962.Maximum%20Width%20Ramp/README.md) - `单调栈`
62+
- [最多能完成排序的块 II](/solution/0700-0799/0768.Max%20Chunks%20To%20Make%20Sorted%20II/README.md) - `单调栈`
6263
- [子数组范围和](/solution/2100-2199/2104.Sum%20of%20Subarray%20Ranges/README.md) - `单调栈`
6364
- [子数组最小乘积的最大值](/solution/1800-1899/1856.Maximum%20Subarray%20Min-Product/README.md) - `单调栈`
6465
- [滑动窗口最大值](/solution/0200-0299/0239.Sliding%20Window%20Maximum/README.md) - `单调队列`

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
5555
- [Design Linked List](/solution/0700-0799/0707.Design%20Linked%20List/README_EN.md) - `Linked List`, `Pointer`, `Array`
5656
- [Next Greater Element I](/solution/0400-0499/0496.Next%20Greater%20Element%20I/README_EN.md) - `Monotonic Stack`
5757
- [Daily Temperatures](/solution/0700-0799/0739.Daily%20Temperatures/README_EN.md) - `Monotonic Stack`
58+
- [Max Chunks To Make Sorted II](/solution/0700-0799/0768.Max%20Chunks%20To%20Make%20Sorted%20II/README_EN.md) - `Monotonic Stack`
5859
- [Sum of Subarray Minimums](/solution/0900-0999/0907.Sum%20of%20Subarray%20Minimums/README_EN.md) - `Monotonic Stack`
5960
- [Maximum Width Ramp](/solution/0900-0999/0962.Maximum%20Width%20Ramp/README_EN.md) - `Monotonic Stack`
6061
- [Sum of Subarray Ranges](/solution/2100-2199/2104.Sum%20of%20Subarray%20Ranges/README_EN.md) - `Monotonic Stack`

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

+77-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46-
单调栈
46+
**方法一:单调栈**
47+
48+
根据题目,我们可以发现,从左到右,每个分块都有一个最大值,并且这些分块的最大值呈单调递增(非严格递增)。我们可以用一个栈来存储这些分块的最大值。最后得到的栈的大小,也就是题目所求的最多能完成排序的块。
49+
50+
时间复杂度 $O(n)$,其中 $n$ 表示 $arr$ 的长度。
4751

4852
<!-- tabs:start -->
4953

@@ -52,15 +56,86 @@
5256
<!-- 这里可写当前语言的特殊实现逻辑 -->
5357

5458
```python
55-
59+
class Solution:
60+
def maxChunksToSorted(self, arr: List[int]) -> int:
61+
stk = []
62+
for v in arr:
63+
if not stk or v >= stk[-1]:
64+
stk.append(v)
65+
else:
66+
mx = stk.pop()
67+
while stk and stk[-1] > v:
68+
stk.pop()
69+
stk.append(mx)
70+
return len(stk)
5671
```
5772

5873
### **Java**
5974

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

6277
```java
78+
class Solution {
79+
public int maxChunksToSorted(int[] arr) {
80+
Deque<Integer> stk = new ArrayDeque<>();
81+
for (int v : arr) {
82+
if (stk.isEmpty() || stk.peek() <= v) {
83+
stk.push(v);
84+
} else {
85+
int mx = stk.pop();
86+
while (!stk.isEmpty() && stk.peek() > v) {
87+
stk.pop();
88+
}
89+
stk.push(mx);
90+
}
91+
}
92+
return stk.size();
93+
}
94+
}
95+
```
6396

97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
int maxChunksToSorted(vector<int>& arr) {
103+
stack<int> stk;
104+
for (int& v : arr)
105+
{
106+
if (stk.empty() || stk.top() <= v) stk.push(v);
107+
else
108+
{
109+
int mx = stk.top();
110+
stk.pop();
111+
while (!stk.empty() && stk.top() > v) stk.pop();
112+
stk.push(mx);
113+
}
114+
}
115+
return stk.size();
116+
}
117+
};
118+
```
119+
120+
### **Go**
121+
122+
```go
123+
func maxChunksToSorted(arr []int) int {
124+
var stk []int
125+
for _, v := range arr {
126+
if len(stk) == 0 || stk[len(stk)-1] <= v {
127+
stk = append(stk, v)
128+
} else {
129+
mx := stk[len(stk)-1]
130+
stk = stk[:len(stk)-1]
131+
for len(stk) > 0 && stk[len(stk)-1] > v {
132+
stk = stk[:len(stk)-1]
133+
}
134+
stk = append(stk, mx)
135+
}
136+
}
137+
return len(stk)
138+
}
64139
```
65140

66141
### **TypeScript**

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

+73-2
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,91 @@ However, splitting into [2, 1], [3], [4], [4] is the highest number of chunks po
4646
### **Python3**
4747

4848
```python
49-
49+
class Solution:
50+
def maxChunksToSorted(self, arr: List[int]) -> int:
51+
stk = []
52+
for v in arr:
53+
if not stk or v >= stk[-1]:
54+
stk.append(v)
55+
else:
56+
mx = stk.pop()
57+
while stk and stk[-1] > v:
58+
stk.pop()
59+
stk.append(mx)
60+
return len(stk)
5061
```
5162

5263
### **Java**
5364

5465
```java
66+
class Solution {
67+
public int maxChunksToSorted(int[] arr) {
68+
Deque<Integer> stk = new ArrayDeque<>();
69+
for (int v : arr) {
70+
if (stk.isEmpty() || stk.peek() <= v) {
71+
stk.push(v);
72+
} else {
73+
int mx = stk.pop();
74+
while (!stk.isEmpty() && stk.peek() > v) {
75+
stk.pop();
76+
}
77+
stk.push(mx);
78+
}
79+
}
80+
return stk.size();
81+
}
82+
}
83+
```
5584

85+
### **C++**
86+
87+
```cpp
88+
class Solution {
89+
public:
90+
int maxChunksToSorted(vector<int>& arr) {
91+
stack<int> stk;
92+
for (int& v : arr)
93+
{
94+
if (stk.empty() || stk.top() <= v) stk.push(v);
95+
else
96+
{
97+
int mx = stk.top();
98+
stk.pop();
99+
while (!stk.empty() && stk.top() > v) stk.pop();
100+
stk.push(mx);
101+
}
102+
}
103+
return stk.size();
104+
}
105+
};
106+
```
107+
108+
### **Go**
109+
110+
```go
111+
func maxChunksToSorted(arr []int) int {
112+
var stk []int
113+
for _, v := range arr {
114+
if len(stk) == 0 || stk[len(stk)-1] <= v {
115+
stk = append(stk, v)
116+
} else {
117+
mx := stk[len(stk)-1]
118+
stk = stk[:len(stk)-1]
119+
for len(stk) > 0 && stk[len(stk)-1] > v {
120+
stk = stk[:len(stk)-1]
121+
}
122+
stk = append(stk, mx)
123+
}
124+
}
125+
return len(stk)
126+
}
56127
```
57128

58129
### **TypeScript**
59130

60131
```ts
61132
function maxChunksToSorted(arr: number[]): number {
62-
let stack = []; // 左进左出
133+
let stack = [];
63134
for (let num of arr) {
64135
if (stack.length && num < stack[0]) {
65136
let max = stack.shift();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int maxChunksToSorted(vector<int>& arr) {
4+
stack<int> stk;
5+
for (int& v : arr)
6+
{
7+
if (stk.empty() || stk.top() <= v) stk.push(v);
8+
else
9+
{
10+
int mx = stk.top();
11+
stk.pop();
12+
while (!stk.empty() && stk.top() > v) stk.pop();
13+
stk.push(mx);
14+
}
15+
}
16+
return stk.size();
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func maxChunksToSorted(arr []int) int {
2+
var stk []int
3+
for _, v := range arr {
4+
if len(stk) == 0 || stk[len(stk)-1] <= v {
5+
stk = append(stk, v)
6+
} else {
7+
mx := stk[len(stk)-1]
8+
stk = stk[:len(stk)-1]
9+
for len(stk) > 0 && stk[len(stk)-1] > v {
10+
stk = stk[:len(stk)-1]
11+
}
12+
stk = append(stk, mx)
13+
}
14+
}
15+
return len(stk)
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int maxChunksToSorted(int[] arr) {
3+
Deque<Integer> stk = new ArrayDeque<>();
4+
for (int v : arr) {
5+
if (stk.isEmpty() || stk.peek() <= v) {
6+
stk.push(v);
7+
} else {
8+
int mx = stk.pop();
9+
while (!stk.isEmpty() && stk.peek() > v) {
10+
stk.pop();
11+
}
12+
stk.push(mx);
13+
}
14+
}
15+
return stk.size();
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def maxChunksToSorted(self, arr: List[int]) -> int:
3+
stk = []
4+
for v in arr:
5+
if not stk or v >= stk[-1]:
6+
stk.append(v)
7+
else:
8+
mx = stk.pop()
9+
while stk and stk[-1] > v:
10+
stk.pop()
11+
stk.append(mx)
12+
return len(stk)

0 commit comments

Comments
 (0)