951. Partition Array into Disjoint Intervals
MediumSolutions 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 us to partition an array nums into two non-empty, contiguous subarrays, left and right. The core condition for a valid partition is that every element in the left subarray must be less than or equal to every element in the right subarray. Our goal is to find a valid partition where the left subarray is as small as possible and return its length.
Solution
Partition Array into Disjoint Intervals in Python
class Solution:
def partitionDisjoint(self, nums: List[int]) -> int:
# The length of the input array.
n = len(nums)
# partition_idx: the index where the left partition ends.
partition_idx = 0
# max_in_left: the maximum value in the current left partition (nums[0...partition_idx]).
max_in_left = nums[0]
# current_max: the maximum value encountered so far in the array traversal (nums[0...i]).
current_max = nums[0]
# Iterate through the array from the second element.
# The loop can go up to n-1 because the right partition must be non-empty.
# A partition at n-2 is the last possible one.
for i in range(1, n):
# Update the maximum value seen so far.
current_max = max(current_max, nums[i])
# If the current element is less than the maximum of the current left partition,
# it means this element must belong to the left partition.
if nums[i] < max_in_left:
# The new partition must be at least at index `i`.
partition_idx = i
# When we extend the left partition to `i`, its new maximum value will be
# the maximum of all elements up to `i`, which is `current_max`.
max_in_left = current_max
# The length of the left partition is partition_idx + 1.
return partition_idx + 1
Partition Array into Disjoint Intervals in Java
class Solution {
public int partitionDisjoint(int[] nums) {
// The length of the input array.
int n = nums.length;
// partitionIdx: the index where the left partition ends.
int partitionIdx = 0;
// maxInLeft: the maximum value in the current left partition (nums[0...partitionIdx]).
int maxInLeft = nums[0];
// currentMax: the maximum value encountered so far in the array traversal (nums[0...i]).
int currentMax = nums[0];
// Iterate through the array from the second element.
// The loop can go up to n-1 because the right partition must be non-empty.
// A partition at n-2 is the last possible one.
for (int i = 1; i < n; ++i) {
// Update the maximum value seen so far.
currentMax = Math.max(currentMax, nums[i]);
// If the current element is less than the maximum of the current left partition,
// it means this element must belong to the left partition.
if (nums[i] < maxInLeft) {
// The new partition must be at least at index `i`.
partitionIdx = i;
// When we extend the left partition to `i`, its new maximum value will be
// the maximum of all elements up to `i`, which is `currentMax`.
maxInLeft = currentMax;
}
}
// The length of the left partition is partitionIdx + 1.
return partitionIdx + 1;
}
}
Partition Array into Disjoint Intervals in C++
class Solution {
public:
int partitionDisjoint(vector<int>& nums) {
// The length of the input array.
int n = nums.size();
// partitionIdx: the index where the left partition ends.
int partitionIdx = 0;
// maxInLeft: the maximum value in the current left partition (nums[0...partitionIdx]).
int maxInLeft = nums[0];
// currentMax: the maximum value encountered so far in the array traversal (nums[0...i]).
int currentMax = nums[0];
// Iterate through the array from the second element.
for (int i = 1; i < n; ++i) {
// Update the maximum value seen so far.
currentMax = std::max(currentMax, nums[i]);
// If the current element is less than the maximum of the current left partition,
// it means this element must belong to the left partition.
// Why? Let's say our current partition is at `partitionIdx`. The max of left is `maxInLeft`.
// If we find an element `nums[i]` which is supposed to be in the right partition
// but `nums[i] < maxInLeft`, it violates the condition that all elements in left
// are less than or equal to all elements in right.
// So, we must extend the left partition to include this element.
if (nums[i] < maxInLeft) {
// The new partition must be at least at index `i`.
partitionIdx = i;
// When we extend the left partition to `i`, its new maximum value will be
// the maximum of all elements up to `i`, which is `currentMax`.
maxInLeft = currentMax;
}
}
// The length of the left partition is partitionIdx + 1.
return partitionIdx + 1;
}
};
Partition Array into Disjoint Intervals in TypeScript
function partitionDisjoint(nums: number[]): number {
// The length of the input array.
const n = nums.length;
// partitionIdx: the index where the left partition ends.
let partitionIdx = 0;
// maxInLeft: the maximum value in the current left partition (nums[0...partitionIdx]).
let maxInLeft = nums[0];
// currentMax: the maximum value encountered so far in the array traversal (nums[0...i]).
let currentMax = nums[0];
// Iterate through the array from the second element.
// The loop can go up to n-1 because the right partition must be non-empty.
// A partition at n-2 is the last possible one.
for (let i = 1; i < n; i++) {
// Update the maximum value seen so far.
currentMax = Math.max(currentMax, nums[i]);
// If the current element is less than the maximum of the current left partition,
// it means this element must belong to the left partition.
if (nums[i] < maxInLeft) {
// The new partition must be at least at index `i`.
partitionIdx = i;
// When we extend the left partition to `i`, its new maximum value will be
// the maximum of all elements up to `i`, which is `currentMax`.
maxInLeft = currentMax;
}
}
// The length of the left partition is partitionIdx + 1.
return partitionIdx + 1;
};
More Medium 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.