MediumNo. 0915

Partition Array into Disjoint Intervals — LeetCode 915 Python Solution

Topics
Array
Reading time
2 min

The problem

Given an integer array nums, partition it into two (contiguous) subarrays left and right so that: Every element in left is less than or equal to every element in right. left and right are non-empty.

Example

Input
nums = [5,0,3,8,6]
Output
3
Explanation
left = [5,0,3], right = [8,6]

Python solution

PythonSolution
class Solution:
    def partitionDisjoint(self, nums: List[int]) -> int:
        n = len(nums)
        mi = [inf] * (n + 1)
        for i in range(n - 1, -1, -1):
            mi[i] = min(nums[i], mi[i + 1])
        mx = 0
        for i, v in enumerate(nums, 1):
            mx = max(mx, v)
            if mx <= mi[i]:
                return i

Complexity

Time
O(n)
Space · auxiliary
O(n)

Related problems

Related problems
No.ProblemDifficulty
0057Insert IntervalMedium
0228Summary RangesEasy
0485Max Consecutive OnesEasy
0665Non-decreasing ArrayMedium
0674Longest Continuous Increasing SubsequenceEasy
0699Falling SquaresHard

Frequently asked questions

How hard is LeetCode 915. Partition Array into Disjoint Intervals?

LeetCode 915. Partition Array into Disjoint Intervals is rated Medium on LeetCode.

What is the time complexity of LeetCode 915. Partition Array into Disjoint Intervals?

The Python solution on this page runs in O(n).

What is the space complexity of LeetCode 915. Partition Array into Disjoint Intervals?

The Python solution on this page uses O(n) auxiliary space.

What topics does LeetCode 915. Partition Array into Disjoint Intervals cover?

LeetCode 915. Partition Array into Disjoint Intervals is tagged Array on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.