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

# Remove Covered Intervals Python Solution

> Tested Python solution for LeetCode 1288 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 1288, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/remove-covered-intervals/description/).

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

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

## Problem

Given an array intervals where intervals\[i] = \[li, ri] represent the interval \[li, ri), remove all intervals that are covered by another interval in the list.

* The interval \[a, b) is covered by the interval \[c, d) if and only if c \<= a and b \<= d.

Return the number of remaining intervals.

### Examples

```
Input: intervals = [[1,4],[3,6],[2,8]]
Output: 2
Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.
```

```
Input: intervals = [[1,4],[2,3]]
Output: 1
```

### Constraints

* 1 \<= intervals.length \<= 1000
* intervals\[i].length == 2
* 0 \<= li \< ri \<= 10^5
* All the given intervals are unique.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    def remove_covered_intervals(self, intervals: list[list[int]]) -> int:
        intervals.sort(key=lambda interval: (interval[0], -interval[1]))
        count = 0
        best_end = -1
        for _, right in intervals:
            if right > best_end:
                count += 1
                best_end = right
        return count
```

## Complexity

| Time | Space |
| - | - |
| - | - |

## Tags

[NeetCode All](/catalog/neetcode).


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