Non-overlapping Intervals
Remove the fewest intervals so the rest don't overlap, by greedily keeping the one that ends first.
The problem
You get an array of intervals [start, end]. Return the smallest number of intervals you must remove so that the remaining ones don't overlap. Intervals that only touch at an end point, like [1, 3] and [3, 5], do not count as overlapping.
Input intervals = [[1, 10], [2, 3], [4, 5], [6, 7]]
Output 1
[1, 10] overlaps all the others; removing just that one leaves three intervals that don't overlap.
Input intervals = [[3, 5], [5, 9], [3, 5]]
Output 1
The two copies of [3, 5] overlap, so one has to go; [5, 9] only touches [3, 5], which is allowed.
Key constraint: Up to 100,000 intervals, so trying subsets is out of the question; one greedy pass after sorting is enough.
How to spot the pattern
- You must drop the fewest intervals to make the rest disjoint, which is the same as keeping the most.
- Choosing which interval to keep first is a scheduling choice, and the earliest end is the safe pick.
The idea
Removing the fewest intervals is the same as keeping the most intervals that don't overlap. Among the intervals that could come first, keep the one that ends earliest: it leaves the most room for everything after it, so no other choice can do better. Sort by end, keep each interval that starts at or after the end of the last kept one, and count every other interval as removed.
Brute force: Try every subset of intervals and keep the largest one with no overlaps, which takes O(2^n) time.
Step by step
- 1Sort the intervals by their end.
- 2Keep the first interval and remember its end as
lastEnd. - 3For each next interval, if its start is at least
lastEnd, keep it and setlastEndto its end. - 4Otherwise it overlaps a kept interval, so add 1 to the removal count.
- 5Return the count.
See the pattern move
The intervals pattern on a small example. Step through it, or press play.
Code
class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
intervals.sort(key=lambda iv: iv[1]) # earliest end first
removed = 0
last_end = intervals[0][1]
for start, end in intervals[1:]:
if start >= last_end:
last_end = end # no overlap: keep it
else:
removed += 1 # overlaps a kept interval
return removedComplexity
Time
O(n log n)
Space
O(1)
Sorting dominates the time; apart from the memory your language's sort uses, the sweep keeps only the last end and a counter.
Watch out for
- Sorting by start and always keeping the earlier interval: in [[1, 10], [2, 3], [4, 5]] that keeps [1, 10] and removes two intervals instead of one.
- Treating touching intervals as overlapping; the check is
start >= lastEnd, so [3, 5] can follow [1, 3]. - Counting the intervals you keep and returning that, instead of the number you remove.
Follow-up questions
Why sort by end and not by length?
A short interval can sit across two intervals that don't overlap each other, like [4, 7] between [1, 5] and [6, 10], so picking the shortest first can cost you two intervals. Picking the earliest end never blocks more than any other choice would.
How many meetings can a single room hold?
That is the same greedy: sort by end and count the intervals you keep instead of the ones you remove.
In Hinglish
Sabse kam hatana matlab sabse zyada rakhna. End ke hisaab se sort karo, jo pehle khatam ho use rakho, aur jo usse takraye use hata do.