Blind 75 · Intervals

Non Overlapping Intervals

Medium

Watch the walkthrough (5:45)

Problem

Given a list of intervals, return the minimum number of intervals to remove so that the rest are pairwise non-overlapping.
Intervals that only touch — [1,2] and [2,3] — do not overlap.

Examples

Example 1:
Input: intervals = [[1,2],[2,3],[3,4],[1,3]]
Output: 1
Explanation: removing [1,3] leaves a clean chain.

Example 2:
Input: intervals = [[1,2],[1,2],[1,2]]
Output: 2

Example 3:
Input: intervals = [[1,2],[2,3]]
Output: 0

Example 4:
Input: intervals = [[1,100],[11,22],[1,11],[2,12]]
Output: 2

Constraints

1 <= intervals.length <= 10^5
intervals[i] is [start, end] with -5 * 10^4 <= start < end <= 5 * 10^4

Prerequisites

Sorting as a setup move — most interval problems are one sort away from a single sweep, and choosing whether to sort by START or by END is the decision that matters.

How to think about it

1. Sort by End, Count the Removals Optimal

Intuition

Removing the fewest intervals is the same as keeping the most. Of the intervals that compete for the same room, keep the one that ends first: it leaves the most room for the rest. So sort the intervals by end and walk them once, holding the last kept end, which starts below every start. An interval that starts at or after the last kept end overlaps nothing kept: it is kept, and the last kept end becomes its end. An interval that starts before the last kept end overlaps the interval kept last, so it is counted as removed. Sorting by start instead is wrong: on [[2,14],[3,5],[6,8],[9,11]] the long interval [2,14] is kept first and the three short intervals inside it are removed, 3, where removing [2,14] alone is enough. Touching does not overlap: an interval that starts exactly at the last kept end is kept, so the check is greater than or equal. Make it strict and touching counts as overlapping: on [[6,9],[2,4],[8,10],[4,7],[4,6]], the interval [4,6], which starts exactly where [2,4] ends, is removed too, and the answer is 3 instead of 2. Sort by end; remove an interval if it starts before the last kept end.

Algorithm

1. Sort the intervals by end.
2. Start the count of removed intervals at 0, and the last kept end below every start.
3. For each interval, if it starts at or after the last kept end, keep it: the last kept end becomes its end.
4. Otherwise it starts before the last kept end: count it as removed.
5. Return the count.

Time & Space

Time O(n log n) for the sort. Space O(1) besides the sort.

2. Keep the Most, Sorted by End Optimal

Intuition

Flip the question: removing the fewest is keeping the most, which is the classic activity-selection problem. Sort by END and greedily keep every interval that starts at or after the last kept end — the earliest-ending choice leaves the most room for what follows, so it is never worse. Sorting by start instead is the trap, and [1,100] is in the tests to punish it.

Algorithm

1. Sort by end time.
2. Keep an interval when it starts at or after the last kept end; update that end.
3. Removals are the total minus the kept count.

Time & Space

Time O(n log n). Space O(1) beyond the sort.