768. Max Chunks To Make Sorted II
Description
You are given an integer array arr.
We split arr into some number of chunks (i.e., partitions), and individually sort each chunk. After concatenating them, the result should equal the sorted array.
Return the largest number of chunks we can make to sort the array.
Β
Example 1:
Input: arr = [5,4,3,2,1] Output: 1 Explanation: Splitting into two or more chunks will not return the required result. For example, splitting into [5, 4], [3, 2, 1] will result in [4, 5, 1, 2, 3], which isn't sorted.
Example 2:
Input: arr = [2,1,3,4,4] Output: 4 Explanation: We can split into two chunks, such as [2, 1], [3, 4, 4]. However, splitting into [2, 1], [3], [4], [4] is the highest number of chunks possible.
Β
Constraints:
1 <= arr.length <= 20000 <= arr[i] <= 108
Solutions
Solution 1: Monotonic Stack
According to the problem, we can find that from left to right, each chunk has a maximum value, and these maximum values are monotonically increasing (non-strictly increasing). We can use a stack to store these maximum values of the chunks. The size of the final stack is the maximum number of chunks that can be sorted.
Time complexity is \(O(n)\), where \(n\) represents the length of \(\textit{arr}\).
1 2 3 4 5 6 7 8 9 10 11 12 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
Solution 2: Prefix Maximums + Suffix Minimums
We would like to partition the array of length \(n\) into several chunks such that after sorting each chunk individually, the entire array remains sorted.
Consider two adjacent chunks:
- left chunk:
left_chunk - right chunk:
right_chunk
If the following condition holds:
max(left_chunk) <= min(right_chunk)
it means:
- every element in the left chunk is less than or equal to every element in the right chunk
- therefore, after sorting both chunks independently, they can still be concatenated into a globally sorted array
Thus, for every index \(i\) satisfying:
we check whether:
holds.
If true, then index \(i\) can serve as a valid partition point.
To efficiently evaluate the above condition, we preprocess:
prefix_maxs[j]
which represents:
i.e. the prefix maximum.
suffix_min[j]
which represents:
i.e. the suffix minimum.
Next:
- Traverse the array from left to right to compute all prefix maximums
- Traverse the array from right to left to compute all suffix minimums
- For each index \(i\), check whether:
prefix_maxs[i - 1] <= suffix_min[i]
holds.
If true, then:
- every element on the left side is less than or equal to every element on the right side
- therefore, the array can be partitioned at index \(i\)
Finally, count all valid partition points.
Note that:
Even if the array is strictly decreasing, the entire array itself can still be treated as one valid chunk.
Therefore, our final answer is equal to all valid partition points + \(1\).
The time complexity is \(O(n)\), and the space complexity is \(O(n)\), where \(n\) represents the length of \(\textit{arr}\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |