Skip to content

Commit 56c3665

Browse files
committedMar 14, 2025
Add solution #768
1 parent 247d2f6 commit 56c3665

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@
582582
765|[Couples Holding Hands](./0765-couples-holding-hands.js)|Hard|
583583
766|[Toeplitz Matrix](./0766-toeplitz-matrix.js)|Easy|
584584
767|[Reorganize String](./0767-reorganize-string.js)|Medium|
585+
768|[Max Chunks To Make Sorted II](./0768-max-chunks-to-make-sorted-ii.js)|Hard|
585586
783|[Minimum Distance Between BST Nodes](./0783-minimum-distance-between-bst-nodes.js)|Easy|
586587
784|[Letter Case Permutation](./0784-letter-case-permutation.js)|Medium|
587588
790|[Domino and Tromino Tiling](./0790-domino-and-tromino-tiling.js)|Medium|
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 768. Max Chunks To Make Sorted II
3+
* https://leetcode.com/problems/max-chunks-to-make-sorted-ii/
4+
* Difficulty: Hard
5+
*
6+
* You are given an integer array arr.
7+
*
8+
* We split arr into some number of chunks (i.e., partitions), and individually sort each chunk.
9+
* After concatenating them, the result should equal the sorted array.
10+
*
11+
* Return the largest number of chunks we can make to sort the array.
12+
*/
13+
14+
/**
15+
* @param {number[]} arr
16+
* @return {number}
17+
*/
18+
var maxChunksToSorted = function(arr) {
19+
const stack = [];
20+
21+
for (const num of arr) {
22+
if (stack.length === 0 || stack[stack.length - 1] <= num) {
23+
stack.push(num);
24+
} else {
25+
const max = stack.pop();
26+
while (stack.length && stack[stack.length - 1] > num) {
27+
stack.pop();
28+
}
29+
stack.push(max);
30+
}
31+
}
32+
33+
return stack.length;
34+
};

0 commit comments

Comments
 (0)
Please sign in to comment.