> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Partition Array into Disjoint Intervals

> Tested Python solution for LeetCode 915 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 915, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array). [View on LeetCode](https://leetcode.com/problems/partition-array-into-disjoint-intervals/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 915   # by problem number
lcpy gen -s partition_array_into_disjoint_intervals   # by problem name
```

## Problem

\<p>Given an integer array \<code>nums\</code>, partition it into two (contiguous) subarrays \<code>left\</code> and \<code>right\</code> so that:\</p>

\<ul>
\<li>Every element in \<code>left\</code> is less than or equal to every element in \<code>right\</code>.\</li>
\<li>\<code>left\</code> and \<code>right\</code> are non-empty.\</li>
\<li>\<code>left\</code> has the smallest possible size.\</li>
\</ul>

\<p>Return \<em>the length of \</em>\<code>left\</code>\<em> after such a partitioning\</em>.\</p>

\<p>Test cases are generated such that partitioning exists.\</p>

### Examples

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

```
Input: nums = [1,1,1,0,6,12]
Output: 4
Explanation: left = [1,1,1,0], right = [6,12]
```

### Constraints

* 2 \<= nums.length \<= 10^5
* 0 \<= nums\[i] \<= 10^6
* There is at least one valid answer for the given input.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/partition_array_into_disjoint_intervals/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/partition_array_into_disjoint_intervals/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def partition_disjoint(self, nums: list[int]) -> int:
        length = 1
        left_max = nums[0]
        cur_max = nums[0]
        for i in range(1, len(nums)):
            cur_max = max(cur_max, nums[i])
            if nums[i] < left_max:
                left_max = cur_max
                length = i + 1
        return length
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.