Max Chunks To Make Sorted II - Problem

Imagine you have an array of integers that needs to be sorted, but instead of sorting it directly, you want to split it into chunks first. Each chunk can be sorted independently, and when you concatenate all the sorted chunks together, the result should be identical to sorting the entire array at once.

Your goal: Find the maximum number of chunks you can create while still achieving a perfectly sorted final array.

Key insight: Unlike the simpler version of this problem, this array can contain duplicate values and the values are not restricted to be a permutation of indices.

Example: For array [2,1,3,4,4], you could split it as [2,1] | [3] | [4,4]. After sorting each chunk: [1,2] + [3] + [4,4] = [1,2,3,4,4], which matches the fully sorted array. This gives us 3 chunks, which is the maximum possible.

Input & Output

example_1.py โ€” Basic Case
$ Input: [5,4,3,8,8]
โ€บ Output: 3
๐Ÿ’ก Note: We can split into [5,4,3] | [8] | [8]. After sorting each chunk: [3,4,5] + [8] + [8] = [3,4,5,8,8], which equals the fully sorted array. This gives us 3 chunks, which is maximum possible.
example_2.py โ€” All Same Elements
$ Input: [2,2,2,2]
โ€บ Output: 4
๐Ÿ’ก Note: Since all elements are the same, we can split into individual chunks: [2] | [2] | [2] | [2]. Each chunk is already sorted, and concatenating gives [2,2,2,2]. Maximum chunks = 4.
example_3.py โ€” Already Sorted
$ Input: [1,2,3,4]
โ€บ Output: 4
๐Ÿ’ก Note: Array is already sorted, so we can split into individual elements: [1] | [2] | [3] | [4]. Each single-element chunk is trivially sorted. Maximum chunks = 4.

Visualization

Tap to expand
๐Ÿ“š Library Book Sorting StrategyBooks to Sort: ["Romeo", "Hamlet", "Gatsby", "Ulysses", "Ulysses"]Pile 1Romeo, Hamlet, Gatsby(max: Romeo)Pile 2Ulysses(max: Ulysses)Pile 3Ulysses(max: Ulysses)After sorting each pile:Gatsby, Hamlet, RomeoUlyssesUlyssesFinal: Gatsby, Hamlet, Romeo, Ulysses, Ulyssesโœ“ Perfect! 3 piles = 3 chunks maximumThe stack tracked maximums: [Romeo, Ulysses, Ulysses]Each stack entry represents one chunk we can create
Understanding the Visualization
1
Identify Break Points
You can only create a new pile when the largest book in current piles won't interfere with future books
2
Use Stack of Maximums
Keep track of the largest book title in each pile using a stack
3
Merge When Necessary
If a new book has a title that comes before books in existing piles, you must merge those piles
4
Count Final Piles
The number of separate piles (stack size) is your answer
Key Takeaway
๐ŸŽฏ Key Insight: We can split after position i only if the maximum value in all chunks up to i won't interfere with future elements. The monotonic stack elegantly tracks these chunk maximums and merges when necessary.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ ร— 2โฟ)

2^(n-1) partitions to try, each requiring O(n log n) sorting and O(n) comparison

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Space for storing chunks and sorted arrays

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค arr.length โ‰ค 2000
  • 0 โ‰ค arr[i] โ‰ค 108
  • Array elements can be duplicated (unlike the simpler version)
  • Array elements are not restricted to be permutations of indices
Asked in
Google 45 Amazon 32 Microsoft 28 Meta 22
52.0K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen