Non-overlapping Intervals
Medium · Intervals
Jump to codeRemove the fewest intervals so the remaining intervals never overlap. Here an interval ending at time t does not conflict with one starting at t. Each interval has start < end.
Examples
Example 1
intervals = [[1,2],[2,3],[3,4],[1,3]]
Output: 1
Example 2
intervals = [[1,2],[1,2],[1,2]]
Output: 2
Constraints
- 1 <= intervals.length <= 1000
- intervals[i].length == 2
- -50000 <= starti < endi <= 50000
Need a hint?
Keeping the interval that finishes earliest leaves the most room.
Real interval interview questions
Questions on the same topic that candidates reported from real company interviews. Some company questions are locked and open with Premium or XP.
- Assign the Minimum Fleet to Rental ReservationsGoogle
- Subtract Blocked Periods from Allowed Deployment WindowsStripe
- Courier Pay From Overlapping Deliveries With Peak-Window MultipliersDoorDash
Solution approach
Sort by end time. Greedily keep each interval whose start is at least the last kept end, then update that end. Count rejections, or subtract the kept count from the original total. Initialize so the first interval is always considered.
Complexity
O(n log n) time; sorting determines auxiliary space.
Python reference solution
def eraseOverlapIntervals(intervals):
end = float('-inf')
removed = 0
for start, finish in sorted(intervals, key=lambda item: item[1]):
if start < end:
removed += 1
else:
end = finish
return removed
JavaScript solution
function eraseOverlapIntervals(intervals) {
intervals.sort((a, b) => a[1] - b[1]);
let end = -Infinity, removed = 0;
for (const [start, finish] of intervals) {
if (start < end) removed++;
else end = finish;
}
return removed;
}
Java solution
import java.util.*;
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
Arrays.sort(intervals, Comparator.comparingInt(interval -> interval[1]));
long end = Long.MIN_VALUE;
int removed = 0;
for (int[] interval : intervals) {
if (interval[0] < end) removed++;
else end = interval[1];
}
return removed;
}
}
C++ solution
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int eraseOverlapIntervals(vector<vector<int>> &intervals) {
sort(intervals.begin(), intervals.end(), [](const auto& a, const auto& b) { return a[1] < b[1]; });
long long end = LLONG_MIN;
int removed = 0;
for (auto& interval : intervals) {
if (interval[0] < end) removed++;
else end = interval[1];
}
return removed;
}
};
Trace a boundary case, then implement the approach in the editor. Submit checks your solution against all 8 test cases.
Keep your reasoning, pseudocode, and edge cases here.
Notes stay in this browser when storage is available.
Accepted submissions mark this problem complete. Progress stays in this browser.
[
[
[
1,
2
],
[
2,
3
],
[
3,
4
],
[
1,
3
]
]
]1
[
[
[
1,
2
],
[
1,
2
],
[
1,
2
]
]
]2
[
[
[
1,
2
],
[
2,
3
]
]
]0
[
[
[
1,
5
],
[
2,
3
]
]
]1
[
[
[
1,
2
],
[
1,
3
],
[
2,
4
]
]
]1
Drafts are saved per language in this browser when storage is available.