Search
⌘K
Intervals

Non-Overlapping Intervals

medium

DESCRIPTION (inspired by Leetcode.com)

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.
][32][41][53][86
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.
Visualization
def nonOverlappingIntervals(intervals):
if not intervals:
return 0
intervals.sort(key=lambda x: x[1])
end = intervals[0][1]
count = 1
for i in range(1, len(intervals)):
# Non-overlapping interval found
if intervals[i][0] >= end:
end = intervals[i][1]
count += 1
return len(intervals) - count
][64][1711][182][107

sort by end time

0 / 1

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.
Visualization
def nonOverlappingIntervals(intervals):
if not intervals:
return 0
intervals.sort(key=lambda x: x[1])
end = intervals[0][1]
count = 1
for i in range(1, len(intervals)):
# Non-overlapping interval found
if intervals[i][0] >= end:
end = intervals[i][1]
count += 1
return len(intervals) - count
][64][1711][182][107endcount: 1

i = 1

0 / 1

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.
Visualization
def nonOverlappingIntervals(intervals):
if not intervals:
return 0
intervals.sort(key=lambda x: x[1])
end = intervals[0][1]
count = 1
for i in range(1, len(intervals)):
# Non-overlapping interval found
if intervals[i][0] >= end:
end = intervals[i][1]
count += 1
return len(intervals) - count
][64][1711][182][107endcount: 2

non-overlapping interval found

0 / 4

Handling non-overlapping intervals.

Solution

|
list of intervals [start, end]
Try these examples:
Visualization
def nonOverlappingIntervals(intervals):
if not intervals:
return 0
intervals.sort(key=lambda x: x[1])
end = intervals[0][1]
count = 1
for i in range(1, len(intervals)):
# Non-overlapping interval found
if intervals[i][0] >= end:
end = intervals[i][1]
count += 1
return len(intervals) - count
][64][1711][182][107

non-overlapping intervals

0 / 8

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.

Unlock Premium Coding Content

Interactive algorithm visualizations
Reading Progress

On This Page