Skip to content

Commit e56bf5f

Browse files
authored
feat: add typescript solutions to lc problems: No.0768, 0769 (doocs#617)
1 parent 9b8cad1 commit e56bf5f

File tree

6 files changed

+106
-0
lines changed

6 files changed

+106
-0
lines changed

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

+22
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444

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

47+
单调栈
48+
4749
<!-- tabs:start -->
4850

4951
### **Python3**
@@ -62,6 +64,26 @@
6264

6365
```
6466

67+
### **TypeScript**
68+
69+
```ts
70+
function maxChunksToSorted(arr: number[]): number {
71+
let stack = []; // 左进左出
72+
for (let num of arr) {
73+
if (stack.length && num < stack[0]) {
74+
let max = stack.shift();
75+
while (stack.length && num < stack[0]) {
76+
stack.shift();
77+
}
78+
stack.unshift(max);
79+
} else {
80+
stack.unshift(num);
81+
}
82+
}
83+
return stack.length;
84+
};
85+
```
86+
6587
### **...**
6688

6789
```

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

+20
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,26 @@ However, splitting into [2, 1], [3], [4], [4] is the highest number of chunks po
9191

9292
```
9393

94+
### **TypeScript**
95+
96+
```ts
97+
function maxChunksToSorted(arr: number[]): number {
98+
let stack = []; // 左进左出
99+
for (let num of arr) {
100+
if (stack.length && num < stack[0]) {
101+
let max = stack.shift();
102+
while (stack.length && num < stack[0]) {
103+
stack.shift();
104+
}
105+
stack.unshift(max);
106+
} else {
107+
stack.unshift(num);
108+
}
109+
}
110+
return stack.length;
111+
};
112+
```
113+
94114
### **...**
95115

96116
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function maxChunksToSorted(arr: number[]): number {
2+
let stack = []; // 左进左出
3+
for (let num of arr) {
4+
if (stack.length && num < stack[0]) {
5+
let max = stack.shift();
6+
while (stack.length && num < stack[0]) {
7+
stack.shift();
8+
}
9+
stack.unshift(max);
10+
} else {
11+
stack.unshift(num);
12+
}
13+
}
14+
return stack.length;
15+
};

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

+18
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@
5858

5959
```
6060

61+
### **TypeScript**
62+
63+
```ts
64+
function maxChunksToSorted(arr: number[]): number {
65+
const n = arr.length;
66+
let ans = 0;
67+
let max = 0;
68+
for (let i = 0; i < n; i++) {
69+
let cur = arr[i];
70+
max = Math.max(cur, max);
71+
if (max == i) {
72+
ans++;
73+
}
74+
}
75+
return ans;
76+
};
77+
```
78+
6179
### **...**
6280

6381
```

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

+18
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,24 @@ However, splitting into [1, 0], [2], [3], [4] is the highest number of chunks po
8484

8585
```
8686

87+
### **TypeScript**
88+
89+
```ts
90+
function maxChunksToSorted(arr: number[]): number {
91+
const n = arr.length;
92+
let ans = 0;
93+
let max = 0;
94+
for (let i = 0; i < n; i++) {
95+
let cur = arr[i];
96+
max = Math.max(cur, max);
97+
if (max == i) {
98+
ans++;
99+
}
100+
}
101+
return ans;
102+
};
103+
```
104+
87105
### **...**
88106

89107
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function maxChunksToSorted(arr: number[]): number {
2+
const n = arr.length;
3+
let ans = 0;
4+
let max = 0;
5+
for (let i = 0; i < n; i++) {
6+
let cur = arr[i];
7+
max = Math.max(cur, max);
8+
if (max == i) {
9+
ans++;
10+
}
11+
}
12+
return ans;
13+
};

0 commit comments

Comments
 (0)