Given an array of intervals intervals
where intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]
, return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Note that intervals which only touch at a point are non-overlapping. For example, [1, 2]
and [2, 3]
are non-overlapping.
Example 1: Input: intervals = [[1,2],[2,3],[3,4],[1,3]] Output: 1 Explanation: [1,3] can be removed and the rest of the intervals are non-overlapping.
Example 2: Input: intervals = [[1,2],[1,2],[1,2]] Output: 2 Explanation: You need to remove two [1,2] to make the rest of the intervals non-overlapping.
Example 3: Input: intervals = [[1,2],[2,3]] Output: 0 Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
Constraints: 1 <= intervals.length <= 10<sup>5</sup> intervals[i].length == 2 -5 * 10<sup>4</sup> <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>4</sup>
from typing import List
class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
# Sort the intervals based on their end times
intervals.sort(key=lambda x: x[1])
count = 0 # Number of intervals to remove
end = float('-inf') # End time of the previously selected non-overlapping interval
for interval in intervals:
start, curr_end = interval
if start >= end:
# Non-overlapping interval found, update the end time
end = curr_end
else:
# Overlapping interval found, increment the count
count += 1
return count
The problem requires finding the minimum number of intervals to remove such that the remaining intervals are non-overlapping. A greedy approach can solve this problem efficiently.
Consider the input intervals = [[1,2],[2,3],[3,4],[1,3]]
.
[[1,2],[2,3],[1,3],[3,4]]
.count = 0
and end = float('-inf')
.[1,2]
: start=1
, end=float('-inf')
. Since 1 >= float('-inf')
, update end = 2
.[2,3]
: start=2
, end=2
. Since 2 >= 2
, update end = 3
.[1,3]
: start=1
, end=3
. Since 1 < 3
, increment count = 1
.[3,4]
: start=3
, end=3
. Since 3 >= 3
, update end = 4
.count = 1
.