From 1a6245d2451215904582e963c7f15136fdfb7ed7 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 8 Jun 2022 19:55:47 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.0768 No.0768.Max Chunks To Make Sorted II --- README.md | 1 + README_EN.md | 1 + .../README.md | 79 ++++++++++++++++++- .../README_EN.md | 75 +++++++++++++++++- .../Solution.cpp | 18 +++++ .../Solution.go | 16 ++++ .../Solution.java | 17 ++++ .../Solution.py | 12 +++ 8 files changed, 215 insertions(+), 4 deletions(-) create mode 100644 solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.cpp create mode 100644 solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.go create mode 100644 solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.java create mode 100644 solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.py diff --git a/README.md b/README.md index d11d6d8fba..c74c154abb 100644 --- a/README.md +++ b/README.md @@ -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) - `单调队列` diff --git a/README_EN.md b/README_EN.md index 68ac5e31fa..1ae7b9974d 100644 --- a/README_EN.md +++ b/README_EN.md @@ -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` diff --git a/solution/0700-0799/0768.Max Chunks To Make Sorted II/README.md b/solution/0700-0799/0768.Max Chunks To Make Sorted II/README.md index 70b71b2929..ea91b55976 100644 --- a/solution/0700-0799/0768.Max Chunks To Make Sorted II/README.md +++ b/solution/0700-0799/0768.Max Chunks To Make Sorted II/README.md @@ -43,7 +43,11 @@ -单调栈 +**方法一:单调栈** + +根据题目,我们可以发现,从左到右,每个分块都有一个最大值,并且这些分块的最大值呈单调递增(非严格递增)。我们可以用一个栈来存储这些分块的最大值。最后得到的栈的大小,也就是题目所求的最多能完成排序的块。 + +时间复杂度 $O(n)$,其中 $n$ 表示 $arr$ 的长度。 @@ -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 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& arr) { + stack 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** diff --git a/solution/0700-0799/0768.Max Chunks To Make Sorted II/README_EN.md b/solution/0700-0799/0768.Max Chunks To Make Sorted II/README_EN.md index 2221973182..ecca7f5c66 100644 --- a/solution/0700-0799/0768.Max Chunks To Make Sorted II/README_EN.md +++ b/solution/0700-0799/0768.Max Chunks To Make Sorted II/README_EN.md @@ -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 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& arr) { + stack 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(); diff --git a/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.cpp b/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.cpp new file mode 100644 index 0000000000..b46ce865aa --- /dev/null +++ b/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int maxChunksToSorted(vector& arr) { + stack 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(); + } +}; \ No newline at end of file diff --git a/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.go b/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.go new file mode 100644 index 0000000000..55e0ee888a --- /dev/null +++ b/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.go @@ -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) +} \ No newline at end of file diff --git a/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.java b/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.java new file mode 100644 index 0000000000..595f928286 --- /dev/null +++ b/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.java @@ -0,0 +1,17 @@ +class Solution { + public int maxChunksToSorted(int[] arr) { + Deque 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(); + } +} \ No newline at end of file diff --git a/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.py b/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.py new file mode 100644 index 0000000000..136132d699 --- /dev/null +++ b/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.py @@ -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)