From 046d0ab6063d69a05ae64042082de1fa0ff52f08 Mon Sep 17 00:00:00 2001 From: rain84 Date: Fri, 20 Dec 2024 01:38:37 +0300 Subject: [PATCH] feat: add solutions to lc problem: No.0769 --- .../0769.Max Chunks To Make Sorted/README.md | 36 +++++++++++++++++++ .../README_EN.md | 36 +++++++++++++++++++ .../Solution2.js | 13 +++++++ .../Solution2.ts | 13 +++++++ 4 files changed, 98 insertions(+) create mode 100644 solution/0700-0799/0769.Max Chunks To Make Sorted/Solution2.js create mode 100644 solution/0700-0799/0769.Max Chunks To Make Sorted/Solution2.ts 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 fd02bac2fc375..e7b8d45997f2d 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 @@ -290,6 +290,42 @@ func maxChunksToSorted(arr []int) int { } ``` +#### TypeScript + +```ts +function maxChunksToSorted(arr: number[]): number { + const stk: number[] = []; + + for (const x of arr) { + if (stk.at(-1)! > x) { + const top = stk.pop()!; + while (stk.length && stk.at(-1)! > x) stk.pop(); + stk.push(top); + } else stk.push(x); + } + + return stk.length; +} +``` + +#### JavaScript + +```js +function maxChunksToSorted(arr) { + const stk = []; + + for (const x of arr) { + if (stk.at(-1) > x) { + const top = stk.pop(); + while (stk.length && stk.at(-1) > x) stk.pop(); + stk.push(top); + } else stk.push(x); + } + + return stk.length; +} +``` + 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 c2d97614d8c40..e43628aea0d2b 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 @@ -275,6 +275,42 @@ func maxChunksToSorted(arr []int) int { } ``` +#### TypeScript + +```ts +function maxChunksToSorted(arr: number[]): number { + const stk: number[] = []; + + for (const x of arr) { + if (stk.at(-1)! > x) { + const top = stk.pop()!; + while (stk.length && stk.at(-1)! > x) stk.pop(); + stk.push(top); + } else stk.push(x); + } + + return stk.length; +} +``` + +#### JavaScript + +```js +function maxChunksToSorted(arr) { + const stk = []; + + for (const x of arr) { + if (stk.at(-1) > x) { + const top = stk.pop(); + while (stk.length && stk.at(-1) > x) stk.pop(); + stk.push(top); + } else stk.push(x); + } + + return stk.length; +} +``` + diff --git a/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution2.js b/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution2.js new file mode 100644 index 0000000000000..8411019770392 --- /dev/null +++ b/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution2.js @@ -0,0 +1,13 @@ +function maxChunksToSorted(arr) { + const stk = []; + + for (const x of arr) { + if (stk.at(-1) > x) { + const top = stk.pop(); + while (stk.length && stk.at(-1) > x) stk.pop(); + stk.push(top); + } else stk.push(x); + } + + return stk.length; +} diff --git a/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution2.ts b/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution2.ts new file mode 100644 index 0000000000000..ab2e0a66c38af --- /dev/null +++ b/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution2.ts @@ -0,0 +1,13 @@ +function maxChunksToSorted(arr: number[]): number { + const stk: number[] = []; + + for (const x of arr) { + if (stk.at(-1)! > x) { + const top = stk.pop()!; + while (stk.length && stk.at(-1)! > x) stk.pop(); + stk.push(top); + } else stk.push(x); + } + + return stk.length; +}