Remove Covered Intervals (LeetCode 1288): Solution and Explanation

Medium
Array
Sorting

Given a list of unique intervals, drop every interval that lies inside another one and count how many remain.

Intuition

Interval [a, b) is covered by [c, d) when c <= a and b <= d. Checking every pair works but costs O(n^2).

If you sort by start, any interval that could cover the current one starts no later, so it has already been seen. Among those, the one that ends furthest is the only one worth comparing against.

Ties on the start need care: of [1,2] and [1,4], the longer one covers the shorter. Sorting equal starts by end descending puts the longer one first, so the shorter one is correctly seen as covered.

So a single running value, the furthest end so far, decides each interval in O(1).

Approach: Sort, then sweep with the furthest end

  1. Sort intervals by start ascending, and by end descending when starts are equal.
  2. Set kept = 0 and reach = 0 (all ends are at least 1).
  3. For each interval, if its end is at most reach, an earlier interval covers it: skip it.
  4. Otherwise it is not covered by anything: increment kept and set reach to its end.
  5. Return kept.

Step-by-step example

intervals = [[1,4],[3,6],[2,8]]
  1. Sorted: [1,4], [2,8], [3,6].
  2. [1,4]: end 4 is past reach 0, so keep it. kept = 1, reach = 4.
  3. [2,8]: end 8 is past reach 4, so keep it. kept = 2, reach = 8.
  4. [3,6]: end 6 is within reach 8, and [2,8] starts earlier, so it is covered.
  5. Return 2.

Code: Python, Java, C++

1class Solution:
2    def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
3        intervals.sort(key=lambda x: (x[0], -x[1]))
4        kept = 0
5        reach = 0
6        for start, end in intervals:
7            if end <= reach:
8                continue
9            kept += 1
10            reach = end
11        return kept
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n log n)

The sort dominates; the sweep is a single O(n) pass.

Space

O(1)

Only two counters beyond the space the sort itself uses.

Other approaches

Check every pair (time O(n^2), space O(1))

For each interval, scan all others for one with start <= its start and end >= its end. Simple and fast enough for n <= 1000.

Sort by start only, track previous interval (time O(n log n), space O(1))

Sort by start ascending and compare each interval to the last kept one, replacing it when the new one has the same start and a larger end. It needs extra handling for equal starts that the end-descending tiebreak avoids.

Common mistakes

  • Sorting equal starts by end ascending. Then [1,2] comes before [1,4], is kept, and the answer is one too high.
  • Using end < reach instead of end <= reach. An interval that ends exactly at the reach is still covered.
  • Comparing only with the previous interval instead of the furthest end so far. A long interval can cover several later ones that do not overlap each other.

Frequently asked questions

Why is the furthest end the only thing to remember?

After sorting, every earlier interval starts at or before the current one. Among them, the one with the largest end covers the current interval whenever any of them does.

Does the half-open [l, r) notation change anything?

No. Coverage is defined purely by c <= a and b <= d, so the same comparisons work for closed and half-open intervals.

Can a removed interval be needed to remove another one?

No. If A covers B and B covers C, then A covers C as well, so removed intervals never matter for later decisions.