> ## 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.

# Non-overlapping Intervals Python Solution

> Tested Python solution for LeetCode 435 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 435, Medium. Topics: Array, Dynamic Programming, Greedy, Sorting. [View on LeetCode](https://leetcode.com/problems/non-overlapping-intervals/description/).

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

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

## Problem

Given an array of intervals intervals where intervals\[i] = \[starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.

Note that intervals which only touch at a point are non-overlapping. For example, \[1, 2] and \[2, 3] are non-overlapping.

### Examples

```
Input: intervals = [[1,2],[2,3],[3,4],[1,3]]
Output: 1
Explanation: [1,3] can be removed and the rest of the intervals are non-overlapping.
```

```
Input: intervals = [[1,2],[1,2],[1,2]]
Output: 2
Explanation: You need to remove two [1,2] to make the rest of the intervals non-overlapping.
```

```
Input: intervals = [[1,2],[2,3]]
Output: 0
Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
```

### Constraints

1 \<= intervals.length \<= 10^5
intervals\[i].length == 2
-5 \* 10^4 \<= starti \< endi \<= 5 \* 10^4

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n) - sorting dominates
    # Space: O(1) - no extra space used
    def erase_overlap_intervals(self, intervals: list[list[int]]) -> int:
        """
        Find minimum number of intervals to remove to make non-overlapping.
        Uses greedy approach: sort by end time and keep intervals with earliest end times.
        """
        if not intervals:
            return 0

        # Sort intervals by end time
        intervals.sort(key=lambda x: x[1])

        count = 0
        prev_end = intervals[0][1]

        for i in range(1, len(intervals)):
            # If current interval starts before previous ends, it overlaps
            if intervals[i][0] < prev_end:
                count += 1  # Remove this interval
            else:
                # No overlap, update previous end time
                prev_end = intervals[i][1]

        return count
```

## Complexity

| Time                           | Space                      |
| ------------------------------ | -------------------------- |
| O(n log n) - sorting dominates | O(1) - no extra space used |

## Tags

[Grind](/catalog/grind), [Blind 75](/catalog/blind-75), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode), [AlgoMaster 75](/catalog/algo-master-75).
