Home
DSA Patterns
Intervals
Non-overlapping Intervals
Non-overlapping Intervals (LeetCode 435)
Non-overlapping Intervals (LeetCode 435): find the minimum number of intervals to remove to eliminate all overlaps.
Problem Understanding
Each entry of intervals is a pair [start, end]. Return the fewest intervals you have to delete so that none of the remaining ones overlap. Intervals that only touch at an endpoint, like [1,2] and [2,3], do not count as overlapping.
Example 1:
- Input:
[[1,2],[2,3],[3,4],[1,3]] - Output:
1 - Explanation: Remove
[1,3]to handle the overlaps with[1,2]and[2,3].
Algorithm Strategy
This is a classic Greedy problem. Removing as few intervals as possible is the same as keeping as many as possible, and the interval that finishes earliest leaves the most room for everything after it.
- Sort by End Time: Sort intervals by
endascending. - Keep the first: It ends earliest, so keep it and set
prevEndto its end. - Check each later interval
curr:- If
curr.start >= prevEnd, it does not overlap the last kept interval: keep it and setprevEnd = curr.end. - Otherwise it overlaps. Because of the sort,
currends no earlier than the interval already kept, so keeping that one never leaves less room: removecurrand add 1 to the count.
- If
- Return the count.
Interactive Visualization
4 INTERVALS
INPUT
intervals · as given
[1,2]
[2,3]
[3,4]
[1,3]
1
4
prevEnd
:
Kept
0
Removed
0
4 intervals: remove as few as possible so none overlap
Next
Sort by end time: [[1,2], [2,3], [1,3], [3,4]]: the interval that finishes first leaves the most room
Intervals
See how sorting by End Time allows us to reliably pick the 'best' intervals to keep.
Dry Run
Input: [[1,2], [2,3], [3,4], [1,3]]
| Step | Interval | End Time | Decision | Count |
|---|---|---|---|---|
| Init | - | - | Sort by End: [[1,2], [2,3], [1,3], [3,4]] |
0 |
| 1 | [1,2] |
2 | Keep. prevEnd = 2. |
0 |
| 2 | [2,3] |
3 | Start 2 >= prev 2. Keep. prevEnd = 3. |
0 |
| 3 | [1,3] |
3 | Start 1 < prev 3. Overlap! Remove. |
1 |
| 4 | [3,4] |
4 | Start 3 >= prev 3. Keep. prevEnd = 4. |
1 |
Edge Cases & Common Mistakes
- Equal End Times: The sort order for same end times doesn't affect correctness for count, though sorting by start (secondary) produces consistent selection.
- Touching Intervals:
[1,2]and[2,3]do NOT overlap. Logic must use>=. - Negative Numbers: Intervals can have negative coordinates.
Solution
Greedy optimization O(N log N).
Non-overlapping Intervals Code
1var eraseOverlapIntervals = function(intervals) {2 if (!intervals.length) return 0;3 intervals.sort((a, b) => a[1] - b[1]);45 let count = 0;6 let prevEnd = intervals[0][1];78 for (let i = 1; i < intervals.length; i++) {9 if (intervals[i][0] >= prevEnd) {10 prevEnd = intervals[i][1];11 } else {12 count++;13 }14 }15 return count;16};
Non-overlapping Intervals in Python, Java, C++, TypeScript and C
Non-overlapping Intervals in Python
def eraseOverlapIntervals(intervals):
if not intervals: return 0
# Sort by end time
intervals.sort(key=lambda x: x[1])
count = 0
prev_end = intervals[0][1]
for i in range(1, len(intervals)):
if intervals[i][0] >= prev_end:
prev_end = intervals[i][1]
else:
count += 1
return countNon-overlapping Intervals in Java
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
if (intervals.length == 0) return 0;
Arrays.sort(intervals, (a, b) -> Integer.compare(a[1], b[1]));
int count = 0;
int prevEnd = intervals[0][1];
for (int i = 1; i < intervals.length; i++) {
if (intervals[i][0] >= prevEnd) {
prevEnd = intervals[i][1];
} else {
count++;
}
}
return count;
}
}Non-overlapping Intervals in C++
class Solution {
public:
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
if (intervals.empty()) return 0;
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b) {
return a[1] < b[1];
});
int count = 0;
int prevEnd = intervals[0][1];
for (int i = 1; i < intervals.size(); i++) {
if (intervals[i][0] >= prevEnd) {
prevEnd = intervals[i][1];
} else {
count++;
}
}
return count;
}
};Non-overlapping Intervals in TypeScript
function eraseOverlapIntervals(intervals: number[][]): number {
if (!intervals.length) return 0;
intervals.sort((a, b) => a[1] - b[1]);
let count = 0;
let prevEnd = intervals[0][1];
for (let i = 1; i < intervals.length; i++) {
if (intervals[i][0] >= prevEnd) {
prevEnd = intervals[i][1];
} else {
count++;
}
}
return count;
};Non-overlapping Intervals in C
int compare(const void* a, const void* b) {
return ((int**)a)[0][1] - ((int**)b)[0][1];
}
int eraseOverlapIntervals(int** intervals, int intervalsSize, int* intervalsColSize) {
if (intervalsSize == 0) return 0;
qsort(intervals, intervalsSize, sizeof(int*), compare);
int count = 0;
int prevEnd = intervals[0][1];
for (int i = 1; i < intervalsSize; i++) {
if (intervals[i][0] >= prevEnd) {
prevEnd = intervals[i][1];
} else {
count++;
}
}
return count;
}More practice: DSA interview questions with answers · patterns cheat sheet
