Skip to content

Commit ee2728e

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

File tree

11 files changed

+234
-93
lines changed

11 files changed

+234
-93
lines changed

solution/0000-0099/0014.Longest Common Prefix/README.md

+24-26
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,19 @@
4949
```python
5050
class Solution:
5151
def longestCommonPrefix(self, strs: List[str]) -> str:
52-
n = len(strs)
53-
if n == 0:
54-
return ''
52+
for l in range(len(strs[0]), -1, -1):
53+
if all(len(s) >= l and s[:l] == strs[0][:l] for s in strs):
54+
return strs[0][:l]
55+
return ''
56+
```
57+
58+
```python
59+
class Solution:
60+
def longestCommonPrefix(self, strs: List[str]) -> str:
5561
for i in range(len(strs[0])):
56-
for j in range(1, n):
57-
if len(strs[j]) <= i or strs[j][i] != strs[0][i]:
58-
return strs[0][:i]
62+
for s in strs[1:]:
63+
if len(s) <= i or s[i] != strs[0][i]:
64+
return s[:i]
5965
return strs[0]
6066
```
6167

@@ -66,8 +72,7 @@ class Solution:
6672
```java
6773
class Solution {
6874
public String longestCommonPrefix(String[] strs) {
69-
int n;
70-
if ((n = strs.length) == 0) return "";
75+
int n = strs.length;
7176
for (int i = 0; i < strs[0].length(); ++i) {
7277
for (int j = 1; j < n; ++j) {
7378
if (strs[j].length() <= i || strs[j].charAt(i) != strs[0].charAt(i)) {
@@ -86,8 +91,7 @@ class Solution {
8691
class Solution {
8792
public:
8893
string longestCommonPrefix(vector<string>& strs) {
89-
int n;
90-
if ((n = strs.size()) == 0) return "";
94+
int n = strs.size();
9195
for (int i = 0; i < strs[0].size(); ++i) {
9296
for (int j = 1; j < n; ++j) {
9397
if (strs[j].size() <= i || strs[j][i] != strs[0][i]) {
@@ -104,24 +108,15 @@ public:
104108
105109
```go
106110
func longestCommonPrefix(strs []string) string {
107-
if len(strs) == 0 {
108-
return ""
109-
}
110-
111-
var b strings.Builder
112-
m, n := len(strs[0]), len(strs)
113-
114-
LOOP:
115-
for i := 0; i < m; i++ {
111+
n := len(strs)
112+
for i := range strs[0] {
116113
for j := 1; j < n; j++ {
117-
if i >= len(strs[j]) || strs[0][i] != strs[j][i] {
118-
break LOOP
114+
if len(strs[j]) <= i || strs[j][i] != strs[0][i] {
115+
return strs[0][:i]
119116
}
120117
}
121-
b.WriteByte(strs[0][i])
122118
}
123-
124-
return b.String()
119+
return strs[0]
125120
}
126121
```
127122

@@ -185,8 +180,11 @@ end
185180
### **JavaScript**
186181

187182
```js
188-
const longestCommonPrefix = function (strs) {
189-
if (strs.length === 0) return '';
183+
/**
184+
* @param {string[]} strs
185+
* @return {string}
186+
*/
187+
var longestCommonPrefix = function(strs) {
190188
for (let j = 0; j < strs[0].length; j++) {
191189
for (let i = 0; i < strs.length; i++) {
192190
if (strs[0][j] !== strs[i][j]) {

solution/0000-0099/0014.Longest Common Prefix/README_EN.md

+24-26
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,19 @@
4242
```python
4343
class Solution:
4444
def longestCommonPrefix(self, strs: List[str]) -> str:
45-
n = len(strs)
46-
if n == 0:
47-
return ''
45+
for l in range(len(strs[0]), -1, -1):
46+
if all(len(s) >= l and s[:l] == strs[0][:l] for s in strs):
47+
return strs[0][:l]
48+
return ''
49+
```
50+
51+
```python
52+
class Solution:
53+
def longestCommonPrefix(self, strs: List[str]) -> str:
4854
for i in range(len(strs[0])):
49-
for j in range(1, n):
50-
if len(strs[j]) <= i or strs[j][i] != strs[0][i]:
51-
return strs[0][:i]
55+
for s in strs[1:]:
56+
if len(s) <= i or s[i] != strs[0][i]:
57+
return s[:i]
5258
return strs[0]
5359
```
5460

@@ -57,8 +63,7 @@ class Solution:
5763
```java
5864
class Solution {
5965
public String longestCommonPrefix(String[] strs) {
60-
int n;
61-
if ((n = strs.length) == 0) return "";
66+
int n = strs.length;
6267
for (int i = 0; i < strs[0].length(); ++i) {
6368
for (int j = 1; j < n; ++j) {
6469
if (strs[j].length() <= i || strs[j].charAt(i) != strs[0].charAt(i)) {
@@ -77,8 +82,7 @@ class Solution {
7782
class Solution {
7883
public:
7984
string longestCommonPrefix(vector<string>& strs) {
80-
int n;
81-
if ((n = strs.size()) == 0) return "";
85+
int n = strs.size();
8286
for (int i = 0; i < strs[0].size(); ++i) {
8387
for (int j = 1; j < n; ++j) {
8488
if (strs[j].size() <= i || strs[j][i] != strs[0][i]) {
@@ -95,24 +99,15 @@ public:
9599
96100
```go
97101
func longestCommonPrefix(strs []string) string {
98-
if len(strs) == 0 {
99-
return ""
100-
}
101-
102-
var b strings.Builder
103-
m, n := len(strs[0]), len(strs)
104-
105-
LOOP:
106-
for i := 0; i < m; i++ {
102+
n := len(strs)
103+
for i := range strs[0] {
107104
for j := 1; j < n; j++ {
108-
if i >= len(strs[j]) || strs[0][i] != strs[j][i] {
109-
break LOOP
105+
if len(strs[j]) <= i || strs[j][i] != strs[0][i] {
106+
return strs[0][:i]
110107
}
111108
}
112-
b.WriteByte(strs[0][i])
113109
}
114-
115-
return b.String()
110+
return strs[0]
116111
}
117112
```
118113

@@ -176,8 +171,11 @@ end
176171
### **JavaScript**
177172

178173
```js
179-
const longestCommonPrefix = function (strs) {
180-
if (strs.length === 0) return '';
174+
/**
175+
* @param {string[]} strs
176+
* @return {string}
177+
*/
178+
var longestCommonPrefix = function(strs) {
181179
for (let j = 0; j < strs[0].length; j++) {
182180
for (let i = 0; i < strs.length; i++) {
183181
if (strs[0][j] !== strs[i][j]) {

solution/0000-0099/0014.Longest Common Prefix/Solution.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
class Solution {
22
public:
33
string longestCommonPrefix(vector<string>& strs) {
4-
int n;
5-
if ((n = strs.size()) == 0) return "";
4+
int n = strs.size();
65
for (int i = 0; i < strs[0].size(); ++i) {
76
for (int j = 1; j < n; ++j) {
87
if (strs[j].size() <= i || strs[j][i] != strs[0][i]) {
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
func longestCommonPrefix(strs []string) string {
2-
if len(strs) == 0 {
3-
return ""
4-
}
5-
6-
var b strings.Builder
7-
m, n := len(strs[0]), len(strs)
8-
9-
LOOP:
10-
for i := 0; i < m; i++ {
2+
n := len(strs)
3+
for i := range strs[0] {
114
for j := 1; j < n; j++ {
12-
if i >= len(strs[j]) || strs[0][i] != strs[j][i] {
13-
break LOOP
5+
if len(strs[j]) <= i || strs[j][i] != strs[0][i] {
6+
return strs[0][:i]
147
}
158
}
16-
b.WriteByte(strs[0][i])
179
}
18-
19-
return b.String()
20-
}
10+
return strs[0]
11+
}

solution/0000-0099/0014.Longest Common Prefix/Solution.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
class Solution {
22
public String longestCommonPrefix(String[] strs) {
3-
int n;
4-
if ((n = strs.length) == 0) return "";
3+
int n = strs.length;
54
for (int i = 0; i < strs[0].length(); ++i) {
65
for (int j = 1; j < n; ++j) {
76
if (strs[j].length() <= i || strs[j].charAt(i) != strs[0].charAt(i)) {

solution/0000-0099/0014.Longest Common Prefix/Solution.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
const longestCommonPrefix = function (strs) {
2-
if (strs.length === 0) return '';
1+
/**
2+
* @param {string[]} strs
3+
* @return {string}
4+
*/
5+
var longestCommonPrefix = function (strs) {
36
for (let j = 0; j < strs[0].length; j++) {
47
for (let i = 0; i < strs.length; i++) {
58
if (strs[0][j] !== strs[i][j]) {
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
class Solution:
22
def longestCommonPrefix(self, strs: List[str]) -> str:
3-
n = len(strs)
4-
if n == 0:
5-
return ''
63
for i in range(len(strs[0])):
7-
for j in range(1, n):
8-
if len(strs[j]) <= i or strs[j][i] != strs[0][i]:
9-
return strs[0][:i]
4+
for s in strs[1:]:
5+
if len(s) <= i or s[i] != strs[0][i]:
6+
return s[:i]
107
return strs[0]

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

+90-6
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,21 @@
4949

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

52-
**方法一:一次遍历**
52+
**方法一:贪心 + 一次遍历**
5353

5454
由于 $arr$ 是 $[0,..,n-1]$ 的一个排列,若已遍历过的数中的最大值 $mx$ 与当前遍历到的下标 $i$ 相等,说明可以进行一次分割,累加答案。
5555

56-
时间复杂度 $O(n)$,其中 $n$ 表示 $arr$ 的长度。
56+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $arr$ 的长度。
57+
58+
**方法二:单调栈**
59+
60+
方法一的解法有一定的局限性,若数组中存在重复元素,就无法得到正确的答案。
61+
62+
根据题目,我们可以发现,从左到右,每个分块都有一个最大值,并且这些分块的最大值呈单调递增。我们可以用一个栈来存储这些分块的最大值。最后得到的栈的大小,也就是题目所求的最多能完成排序的块。
63+
64+
以上这种解法,不仅可以解决本题,也可以解决 [768. 最多能完成排序的块 II](https://leetcode-cn.com/problems/max-chunks-to-make-sorted-ii/) 这道困难题。大家可以自行尝试。
65+
66+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $arr$ 的长度。
5767

5868
<!-- tabs:start -->
5969

@@ -72,15 +82,29 @@ class Solution:
7282
return ans
7383
```
7484

85+
```python
86+
class Solution:
87+
def maxChunksToSorted(self, arr: List[int]) -> int:
88+
stk = []
89+
for v in arr:
90+
if not stk or v >= stk[-1]:
91+
stk.append(v)
92+
else:
93+
mx = stk.pop()
94+
while stk and stk[-1] > v:
95+
stk.pop()
96+
stk.append(mx)
97+
return len(stk)
98+
```
99+
75100
### **Java**
76101

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

79104
```java
80105
class Solution {
81106
public int maxChunksToSorted(int[] arr) {
82-
int ans = 0;
83-
int mx = 0;
107+
int ans = 0, mx = 0;
84108
for (int i = 0; i < arr.length; ++i) {
85109
mx = Math.max(mx, arr[i]);
86110
if (i == mx) {
@@ -92,14 +116,33 @@ class Solution {
92116
}
93117
```
94118

119+
```java
120+
class Solution {
121+
public int maxChunksToSorted(int[] arr) {
122+
Deque<Integer> stk = new ArrayDeque<>();
123+
for (int v : arr) {
124+
if (stk.isEmpty() || v >= stk.peek()) {
125+
stk.push(v);
126+
} else {
127+
int mx = stk.pop();
128+
while (!stk.isEmpty() && stk.peek() > v) {
129+
stk.pop();
130+
}
131+
stk.push(mx);
132+
}
133+
}
134+
return stk.size();
135+
}
136+
}
137+
```
138+
95139
### **C++**
96140

97141
```cpp
98142
class Solution {
99143
public:
100144
int maxChunksToSorted(vector<int>& arr) {
101-
int ans = 0;
102-
int mx = 0;
145+
int ans = 0, mx = 0;
103146
for (int i = 0; i < arr.size(); ++i) {
104147
mx = max(mx, arr[i]);
105148
ans += i == mx;
@@ -109,6 +152,28 @@ public:
109152
};
110153
```
111154
155+
```cpp
156+
class Solution {
157+
public:
158+
int maxChunksToSorted(vector<int>& arr) {
159+
stack<int> stk;
160+
for (int v : arr) {
161+
if (stk.empty() || v >= stk.top()) {
162+
stk.push(v);
163+
} else {
164+
int mx = stk.top();
165+
stk.pop();
166+
while (!stk.empty() && stk.top() > v) {
167+
stk.pop();
168+
}
169+
stk.push(mx);
170+
}
171+
}
172+
return stk.size();
173+
}
174+
};
175+
```
176+
112177
### **Go**
113178

114179
```go
@@ -131,6 +196,25 @@ func max(a, b int) int {
131196
}
132197
```
133198

199+
```go
200+
func maxChunksToSorted(arr []int) int {
201+
stk := []int{}
202+
for _, v := range arr {
203+
if len(stk) == 0 || v >= stk[len(stk)-1] {
204+
stk = append(stk, v)
205+
} else {
206+
mx := stk[len(stk)-1]
207+
stk = stk[:len(stk)-1]
208+
for len(stk) > 0 && stk[len(stk)-1] > v {
209+
stk = stk[:len(stk)-1]
210+
}
211+
stk = append(stk, mx)
212+
}
213+
}
214+
return len(stk)
215+
}
216+
```
217+
134218
### **TypeScript**
135219

136220
```ts

0 commit comments

Comments
 (0)