Write a function to return the minimum number of intervals that must be removed from a given array intervals, where intervals[i] consists of a starting point starti and an ending point endi, to ensure that the remaining intervals do not overlap.
Input:
intervals = [[1,3],[5,8],[4,10],[11,13]]
Output:
1
Explanation: Removing the interval [4,10] leaves all other intervals non-overlapping.
Explanation
This question reduces to finding the maximum number of non-overlapping intervals. Once we know that value, then we can subtract it from the total number of intervals to get the minimum number of intervals that need to be removed.
If we remove [1, 4], then all the remaining intervals are non-overlapping.
To find the maximum number of non-overlapping intervals, we can sort the intervals by their end time. We then use a greedy approach: we iterate over each sorted interval, and repeatedly try to add that interval to the set of non-overlapping intervals. Sorting by the end time allows us to choose the intervals that end the earliest first, which frees up more time for intervals to be included later.
We start by keeping track of a variable end which represents the end time of the latest interval in our set of non-overlapping intervals, as well as a variable count which represents the number of non-overlapping intervals we have found so far.
defnonOverlappingIntervals(intervals):
ifnot intervals:
return0
intervals.sort(key=lambda x: x[1])
end = intervals[0][1]
count =1
for i inrange(1,len(intervals)):
# Non-overlapping interval found
if intervals[i][0]>= end:
end = intervals[i][1]
count +=1
returnlen(intervals)- count
sort by end time
0 / 1
Python
We then iterate over each interval starting from the second interval in the list (the first interval is always non-overlapping). For each interval, we compare the start time of the interval to end. If it is less than end, then we cannot add the interval to our set of non-overlapping intervals, so we move onto the next interval without updating end or count.
defnonOverlappingIntervals(intervals):
ifnot intervals:
return0
intervals.sort(key=lambda x: x[1])
end = intervals[0][1]
count =1
for i inrange(1,len(intervals)):
# Non-overlapping interval found
if intervals[i][0]>= end:
end = intervals[i][1]
count +=1
returnlen(intervals)- count
i = 1
0 / 1
Python
We cannot add [1, 4] to the set of non-overlapping intervals.
If it is greater than or equal to end, then we can add the interval to our set of non-overlapping intervals by updating count. We then update the value of end to be the end time of the current interval.
defnonOverlappingIntervals(intervals):
ifnot intervals:
return0
intervals.sort(key=lambda x: x[1])
end = intervals[0][1]
count =1
for i inrange(1,len(intervals)):
# Non-overlapping interval found
if intervals[i][0]>= end:
end = intervals[i][1]
count +=1
returnlen(intervals)- count
non-overlapping interval found
0 / 4
Python
Handling non-overlapping intervals.
Solution
defnonOverlappingIntervals(intervals):
ifnot intervals:
return0
intervals.sort(key=lambda x: x[1])
end = intervals[0][1]
count =1
for i inrange(1,len(intervals)):
# Non-overlapping interval found
if intervals[i][0]>= end:
end = intervals[i][1]
count +=1
returnlen(intervals)- count
non-overlapping intervals
0 / 8
Python
Test Your Knowledge
Login to take the complexity quiz and track your progress
Complexity Analysis
Time Complexity: O(n * logn) where n is the number of intervals. The time complexity is dominated by the sorting step.
Space Complexity: O(1) We only initialize two extra variables regardless of the input size.
Your account is free and you can post anonymously if you choose.