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 05145a0172102..96f57ebd65cfa 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 @@ -44,6 +44,8 @@ +单调栈 + ### **Python3** @@ -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; +}; +``` + ### **...** ``` 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 dfa1048ff8987..0daa181b75be2 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 @@ -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; +}; +``` + ### **...** ``` diff --git a/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.ts b/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.ts new file mode 100644 index 0000000000000..b2516b0014d7f --- /dev/null +++ b/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.ts @@ -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; +}; \ No newline at end of file diff --git a/solution/0700-0799/0769.Max Chunks To Make Sorted/README.md b/solution/0700-0799/0769.Max Chunks To Make Sorted/README.md index 02c38f48ae0fa..29e1f11503509 100644 --- a/solution/0700-0799/0769.Max Chunks To Make Sorted/README.md +++ b/solution/0700-0799/0769.Max Chunks To Make Sorted/README.md @@ -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; +}; +``` + ### **...** ``` diff --git a/solution/0700-0799/0769.Max Chunks To Make Sorted/README_EN.md b/solution/0700-0799/0769.Max Chunks To Make Sorted/README_EN.md index 4a686ac3b7901..288f1af3c7bd6 100644 --- a/solution/0700-0799/0769.Max Chunks To Make Sorted/README_EN.md +++ b/solution/0700-0799/0769.Max Chunks To Make Sorted/README_EN.md @@ -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; +}; +``` + ### **...** ``` diff --git a/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.ts b/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.ts new file mode 100644 index 0000000000000..9c408de022aca --- /dev/null +++ b/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.ts @@ -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; +}; \ No newline at end of file