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; +}