779. Max Chunks To Make Sorted II
HardSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the maximum number of chunks an array arr can be partitioned into, such that if we sort each chunk individually and then concatenate them, the result is the sorted version of the original array.
Solution
Max Chunks To Make Sorted II in Python
class Solution:
def maxChunksToSorted(self, arr: List[int]) -> int:
n = len(arr)
if n <= 1:
return 1
# min_from_right[i] will store the minimum element in arr[i...n-1]
min_from_right = [0] * n
min_from_right[n - 1] = arr[n - 1]
for i in range(n - 2, -1, -1):
min_from_right[i] = min(arr[i], min_from_right[i + 1])
chunks = 1 # The whole array is at least one chunk
max_so_far = -1 # arr[i] >= 0
# Iterate through the array to find split points. A split is possible after index i.
for i in range(n - 1):
# Update max_so_far for the current left partition arr[0...i]
max_so_far = max(max_so_far, arr[i])
# If the max of the left part (arr[0...i]) is <= the min of the right part (arr[i+1...n-1]),
# we can make a cut here.
if max_so_far <= min_from_right[i + 1]:
chunks += 1
return chunks
Max Chunks To Make Sorted II in Java
class Solution {
public int maxChunksToSorted(int[] arr) {
int n = arr.length;
if (n <= 1) {
return 1;
}
// min_from_right[i] will store the minimum element in arr[i...n-1]
int[] min_from_right = new int[n];
min_from_right[n - 1] = arr[n - 1];
for (int i = n - 2; i >= 0; i--) {
min_from_right[i] = Math.min(arr[i], min_from_right[i + 1]);
}
int chunks = 1; // The whole array is at least one chunk
int max_so_far = Integer.MIN_VALUE;
// Iterate through the array to find split points. A split is possible after index i.
for (int i = 0; i < n - 1; i++) {
// Update max_so_far for the current left partition arr[0...i]
max_so_far = Math.max(max_so_far, arr[i]);
// If the max of the left part (arr[0...i]) is <= the min of the right part (arr[i+1...n-1]),
// we can make a cut here.
if (max_so_far <= min_from_right[i + 1]) {
chunks++;
}
}
return chunks;
}
}
Max Chunks To Make Sorted II in C++
class Solution {
public:
int maxChunksToSorted(vector<int>& arr) {
int n = arr.size();
if (n <= 1) {
return 1;
}
// min_from_right[i] will store the minimum element in arr[i...n-1]
vector<int> min_from_right(n);
min_from_right[n - 1] = arr[n - 1];
for (int i = n - 2; i >= 0; --i) {
min_from_right[i] = min(arr[i], min_from_right[i + 1]);
}
int chunks = 1; // The whole array is at least one chunk
int max_so_far = arr[0];
// Iterate through the array to find split points. A split is possible after index i.
for (int i = 0; i < n - 1; ++i) {
// Update max_so_far for the current left partition arr[0...i]
max_so_far = max(max_so_far, arr[i]);
// If the max of the left part (arr[0...i]) is <= the min of the right part (arr[i+1...n-1]),
// we can make a cut here.
if (max_so_far <= min_from_right[i + 1]) {
chunks++;
}
}
return chunks;
}
};
Max Chunks To Make Sorted II in TypeScript
function maxChunksToSorted(arr: number[]): number {
const n = arr.length;
if (n <= 1) {
return 1;
}
// min_from_right[i] will store the minimum element in arr[i...n-1]
const min_from_right = new Array<number>(n);
min_from_right[n - 1] = arr[n - 1];
for (let i = n - 2; i >= 0; i--) {
min_from_right[i] = Math.min(arr[i], min_from_right[i + 1]);
}
let chunks = 1; // The whole array is at least one chunk
let max_so_far = -Infinity;
// Iterate through the array to find split points. A split is possible after index i.
for (let i = 0; i < n - 1; i++) {
// Update max_so_far for the current left partition arr[0...i]
max_so_far = Math.max(max_so_far, arr[i]);
// If the max of the left part (arr[0...i]) is <= the min of the right part (arr[i+1...n-1]),
// we can make a cut here.
if (max_so_far <= min_from_right[i + 1]) {
chunks++;
}
}
return chunks;
};
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.