Skip to content

feat: add typescript solution to lc problem: No.0768、0769 #617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions solution/0700-0799/0768.Max Chunks To Make Sorted II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

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

单调栈

<!-- tabs:start -->

### **Python3**
Expand All @@ -62,6 +64,26 @@

```

### **TypeScript**

```ts
function maxChunksToSorted(arr: number[]): number {
let stack = []; // 左进左出
for (let num of arr) {
if (stack.length && num < stack[0]) {
let max = stack.shift();
while (stack.length && num < stack[0]) {
stack.shift();
}
stack.unshift(max);
} else {
stack.unshift(num);
}
}
return stack.length;
};
```

### **...**

```
Expand Down
20 changes: 20 additions & 0 deletions solution/0700-0799/0768.Max Chunks To Make Sorted II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,26 @@ However, splitting into [2, 1], [3], [4], [4] is the highest number of chunks po

```

### **TypeScript**

```ts
function maxChunksToSorted(arr: number[]): number {
let stack = []; // 左进左出
for (let num of arr) {
if (stack.length && num < stack[0]) {
let max = stack.shift();
while (stack.length && num < stack[0]) {
stack.shift();
}
stack.unshift(max);
} else {
stack.unshift(num);
}
}
return stack.length;
};
```

### **...**

```
Expand Down
15 changes: 15 additions & 0 deletions solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function maxChunksToSorted(arr: number[]): number {
let stack = []; // 左进左出
for (let num of arr) {
if (stack.length && num < stack[0]) {
let max = stack.shift();
while (stack.length && num < stack[0]) {
stack.shift();
}
stack.unshift(max);
} else {
stack.unshift(num);
}
}
return stack.length;
};
18 changes: 18 additions & 0 deletions solution/0700-0799/0769.Max Chunks To Make Sorted/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@

```

### **TypeScript**

```ts
function maxChunksToSorted(arr: number[]): number {
const n = arr.length;
let ans = 0;
let max = 0;
for (let i = 0; i < n; i++) {
let cur = arr[i];
max = Math.max(cur, max);
if (max == i) {
ans++;
}
}
return ans;
};
```

### **...**

```
Expand Down
18 changes: 18 additions & 0 deletions solution/0700-0799/0769.Max Chunks To Make Sorted/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ However, splitting into [1, 0], [2], [3], [4] is the highest number of chunks po

```

### **TypeScript**

```ts
function maxChunksToSorted(arr: number[]): number {
const n = arr.length;
let ans = 0;
let max = 0;
for (let i = 0; i < n; i++) {
let cur = arr[i];
max = Math.max(cur, max);
if (max == i) {
ans++;
}
}
return ans;
};
```

### **...**

```
Expand Down
13 changes: 13 additions & 0 deletions solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function maxChunksToSorted(arr: number[]): number {
const n = arr.length;
let ans = 0;
let max = 0;
for (let i = 0; i < n; i++) {
let cur = arr[i];
max = Math.max(cur, max);
if (max == i) {
ans++;
}
}
return ans;
};