Given an integer array nums, split it into two contiguous parts, left (a prefix) and right (the rest of the array), so that:
-
Every element in
left
is less than or equal to every element in
right
.
-
left
and
right
are both non-empty.
-
left
has the smallest possible size.
Return the length of left after such a split.
Function Signature
def partition_disjoint(nums: list[int]) -> int:
Rules
-
left
is
nums[0:k]
and
right
is
nums[k:]
for some
k
with
1 <= k <= len(nums) - 1
. Return that
k
.
-
The condition is
max(left) <= min(right)
, so equal values may sit on both sides.
-
Because
left
must be as short as possible, the answer is unique.
Constraints
-
2 <= len(nums) <= 10^5
-
0 <= nums[i] <= 10^6
-
There is at least one valid split for the given input.
Examples
Input: nums = [5, 0, 3, 8, 6]
Output: 3
With left = [5, 0, 3] and right = [8, 6], the largest left value is 5 and the smallest right value is 6. A shorter left fails because the 0 or the 3 would end up on the right side, below 5.
Input: nums = [1, 1, 1, 0, 6, 12]
Output: 4
The 0 at index 3 is smaller than the leading 1s, so it has to be in left. Then left = [1, 1, 1, 0] has max 1 and right = [6, 12] has min 6.
Input: nums = [2, 1, 3, 0, 5]
Output: 4
The 0 at index 3 forces left to reach index 3, which also pulls the 3 at index 2 into left, so the left maximum is 3. With right = [5], the condition 3 <= 5 holds.