feat: add solutions to lc problem: No.0768

No.0768.Max Chunks To Make Sorted II
This commit is contained in:
yanglbme 2022-06-08 19:55:47 +08:00
parent 5a493f1ab7
commit 1a6245d245
8 changed files with 215 additions and 4 deletions

View File

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

View File

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

View File

@ -43,7 +43,11 @@
<!-- 这里可写通用的实现逻辑 -->
单调栈
**方法一:单调栈**
根据题目,我们可以发现,从左到右,每个分块都有一个最大值,并且这些分块的最大值呈单调递增(非严格递增)。我们可以用一个栈来存储这些分块的最大值。最后得到的栈的大小,也就是题目所求的最多能完成排序的块。
时间复杂度 $O(n)$,其中 $n$ 表示 $arr$ 的长度。
<!-- tabs:start -->
@ -52,7 +56,18 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->
```python
class Solution:
def maxChunksToSorted(self, arr: List[int]) -> int:
stk = []
for v in arr:
if not stk or v >= stk[-1]:
stk.append(v)
else:
mx = stk.pop()
while stk and stk[-1] > v:
stk.pop()
stk.append(mx)
return len(stk)
```
### **Java**
@ -60,7 +75,67 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->
```java
class Solution {
public int maxChunksToSorted(int[] arr) {
Deque<Integer> stk = new ArrayDeque<>();
for (int v : arr) {
if (stk.isEmpty() || stk.peek() <= v) {
stk.push(v);
} else {
int mx = stk.pop();
while (!stk.isEmpty() && stk.peek() > v) {
stk.pop();
}
stk.push(mx);
}
}
return stk.size();
}
}
```
### **C++**
```cpp
class Solution {
public:
int maxChunksToSorted(vector<int>& arr) {
stack<int> stk;
for (int& v : arr)
{
if (stk.empty() || stk.top() <= v) stk.push(v);
else
{
int mx = stk.top();
stk.pop();
while (!stk.empty() && stk.top() > v) stk.pop();
stk.push(mx);
}
}
return stk.size();
}
};
```
### **Go**
```go
func maxChunksToSorted(arr []int) int {
var stk []int
for _, v := range arr {
if len(stk) == 0 || stk[len(stk)-1] <= v {
stk = append(stk, v)
} else {
mx := stk[len(stk)-1]
stk = stk[:len(stk)-1]
for len(stk) > 0 && stk[len(stk)-1] > v {
stk = stk[:len(stk)-1]
}
stk = append(stk, mx)
}
}
return len(stk)
}
```
### **TypeScript**

View File

@ -46,20 +46,91 @@ However, splitting into [2, 1], [3], [4], [4] is the highest number of chunks po
### **Python3**
```python
class Solution:
def maxChunksToSorted(self, arr: List[int]) -> int:
stk = []
for v in arr:
if not stk or v >= stk[-1]:
stk.append(v)
else:
mx = stk.pop()
while stk and stk[-1] > v:
stk.pop()
stk.append(mx)
return len(stk)
```
### **Java**
```java
class Solution {
public int maxChunksToSorted(int[] arr) {
Deque<Integer> stk = new ArrayDeque<>();
for (int v : arr) {
if (stk.isEmpty() || stk.peek() <= v) {
stk.push(v);
} else {
int mx = stk.pop();
while (!stk.isEmpty() && stk.peek() > v) {
stk.pop();
}
stk.push(mx);
}
}
return stk.size();
}
}
```
### **C++**
```cpp
class Solution {
public:
int maxChunksToSorted(vector<int>& arr) {
stack<int> stk;
for (int& v : arr)
{
if (stk.empty() || stk.top() <= v) stk.push(v);
else
{
int mx = stk.top();
stk.pop();
while (!stk.empty() && stk.top() > v) stk.pop();
stk.push(mx);
}
}
return stk.size();
}
};
```
### **Go**
```go
func maxChunksToSorted(arr []int) int {
var stk []int
for _, v := range arr {
if len(stk) == 0 || stk[len(stk)-1] <= v {
stk = append(stk, v)
} else {
mx := stk[len(stk)-1]
stk = stk[:len(stk)-1]
for len(stk) > 0 && stk[len(stk)-1] > v {
stk = stk[:len(stk)-1]
}
stk = append(stk, mx)
}
}
return len(stk)
}
```
### **TypeScript**
```ts
function maxChunksToSorted(arr: number[]): number {
let stack = []; // 左进左出
let stack = [];
for (let num of arr) {
if (stack.length && num < stack[0]) {
let max = stack.shift();

View File

@ -0,0 +1,18 @@
class Solution {
public:
int maxChunksToSorted(vector<int>& arr) {
stack<int> stk;
for (int& v : arr)
{
if (stk.empty() || stk.top() <= v) stk.push(v);
else
{
int mx = stk.top();
stk.pop();
while (!stk.empty() && stk.top() > v) stk.pop();
stk.push(mx);
}
}
return stk.size();
}
};

View File

@ -0,0 +1,16 @@
func maxChunksToSorted(arr []int) int {
var stk []int
for _, v := range arr {
if len(stk) == 0 || stk[len(stk)-1] <= v {
stk = append(stk, v)
} else {
mx := stk[len(stk)-1]
stk = stk[:len(stk)-1]
for len(stk) > 0 && stk[len(stk)-1] > v {
stk = stk[:len(stk)-1]
}
stk = append(stk, mx)
}
}
return len(stk)
}

View File

@ -0,0 +1,17 @@
class Solution {
public int maxChunksToSorted(int[] arr) {
Deque<Integer> stk = new ArrayDeque<>();
for (int v : arr) {
if (stk.isEmpty() || stk.peek() <= v) {
stk.push(v);
} else {
int mx = stk.pop();
while (!stk.isEmpty() && stk.peek() > v) {
stk.pop();
}
stk.push(mx);
}
}
return stk.size();
}
}

View File

@ -0,0 +1,12 @@
class Solution:
def maxChunksToSorted(self, arr: List[int]) -> int:
stk = []
for v in arr:
if not stk or v >= stk[-1]:
stk.append(v)
else:
mx = stk.pop()
while stk and stk[-1] > v:
stk.pop()
stk.append(mx)
return len(stk)