From fb04d9e511d2cb03810d47ba39b6f823e5a9d56a Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Wed, 24 Nov 2021 00:09:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20typescript=20solution=20to=20lc?= =?UTF-8?q?=20problem:=20No.0768=E3=80=810769?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../README.md | 22 +++++++++++++++++++ .../README_EN.md | 20 +++++++++++++++++ .../Solution.ts | 15 +++++++++++++ .../0769.Max Chunks To Make Sorted/README.md | 18 +++++++++++++++ .../README_EN.md | 18 +++++++++++++++ .../Solution.ts | 13 +++++++++++ 6 files changed, 106 insertions(+) create mode 100644 solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.ts create mode 100644 solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.ts 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