Non-overlapping Intervals
Non-overlapping Intervals is LeetCode 435 (Medium). Given a list of intervals [start, end], return the fewest intervals you must remove so that none of the remaining ones overlap.
- Intervals that only touch, like
[1, 2]and[2, 3], do not overlap. - There can be 10⁵ intervals, so trying different subsets is out of the question. One sort and one pass are enough.
- 1 <= intervals.length <= 10⁵
- intervals[i].length == 2
- -5 × 10⁴ <= startᵢ < endᵢ <= 5 × 10⁴
Intuition
Removing the fewest intervals is the same as keeping the most: the answer is n minus the size of the largest set with no overlaps.
To keep the most, always keep the interval that ends first among those that still fit. This is safe. Take any best set and look at its earliest interval. Swap it for the interval with the earliest end overall: that one ends no later, so it cannot clash with anything else in the set, and the set is still valid and just as large. The same argument then applies to everything after it. An early end leaves the most room for the rest.
"Remove the fewest" or "keep the most" intervals so none overlap is interval scheduling. Sorting by end time and taking whatever fits is the standard greedy for it.
Approach
Before reading on, decide which interval you would keep first if you could keep only intervals that do not clash. Try both the earliest start and the earliest end on a long interval with three short ones inside it. Aim for O(n log n).
Sort by end
Sort the intervals by their end value. Among intervals with the same end, it does not matter which comes first: whichever one is kept blocks the future in exactly the same way.
Remember where the last kept interval ends
Keep end, the end of the last interval kept, starting at minus infinity so that the first interval is always kept. Count removals in removed.
Keep an interval that starts at or after end
If start >= end, the interval fits after everything kept so far. Keep it and move end to its end. The comparison is >= because touching intervals do not overlap.
Otherwise remove it
If it starts before end, it clashes with the last kept interval. It also ends no earlier, since the list is sorted by end, so keeping it instead could only leave less room. Remove it and leave end where it is.
Code in Python, C++ and Java
Common pitfalls
Sorting by start and keeping the first one
intervals.sort()
intervals.sort(key=lambda iv: iv[1])
Sorted by start, a long interval that starts early is kept and blocks everything inside it. For [[1, 11], [2, 3], [4, 5], [6, 7]] that removes the three short intervals (3), while removing only [1, 11] is enough (1).
Treating touching intervals as overlapping
if start > end:
if start >= end:
For [[1, 2], [2, 3]] the strict test removes [2, 3] and returns 1, but intervals that share only an endpoint are allowed together, so the answer is 0.
Moving end when an interval is removed
else:
removed += 1
end = finishelse:
removed += 1A removed interval no longer takes up space. For [[1, 3], [2, 4], [3, 5]], moving end to 4 after removing [2, 4] makes [3, 5] look like a clash, giving 2 instead of 1.
Edge cases
Starts can go down to −5 × 10⁴. Starting end at 0 would remove [-5, -1] for no reason, so it starts at minus infinity (INT_MIN in C++, Integer.MIN_VALUE in Java).
Complexity
Non-overlapping Intervals FAQ
Can I sort by start time instead?
Yes, if on every clash you keep the one of the two that ends sooner: count a removal and set end = min(end, finish). Sorting by end makes that choice automatic, so the loop has one less thing to get right.
Is this the activity selection problem?
Yes. N Meetings in One Room asks for the number of meetings you can keep; here you report how many are left out, which is n minus that number.
How does it relate to Minimum Number of Arrows to Burst Balloons?
That problem uses the same sort by end. There, balloons that touch can share one arrow, so the test becomes strictly greater and you count groups instead of removals.