Search
⌘K
Get Premium
Intervals

Intervals Overview


This page covers problems involving intervals, which are given as a list of [start, end] times.
][71][129][63][118
Sorting intervals by start time.
Interval problems typically involve sorting the given intervals, and then processing each interval in sorted each order. On this page, we'll cover:
  1. Sorting intervals by start time
  2. Sorting intervals by end time

Sorting by Start Time

Sorting intervals by their start times makes it easy to merge two intervals that are overlapping.
][31][62][108][1815merged][31
Merging two overlapping intervals.

Overlapping Intervals

After sorting by start time, an interval overlaps with the previous interval if it starts before the end time of the previous interval.
][20][83][106][1512
Overlapping intervals shown in green.
Detecting overlapping intervals is the basis of the question Can Attend Meetings, in which we are given a list of intervals representing the start and end times of meetings, and we need to determine if a person can attend all meetings.
We sort the intervals by their start times and iterate over each meeting. If the current meeting overlaps with the previous one, we return False. If we make it through the entire list without finding any overlaps, we return True.
|
list of intervals [start, end]
Try these examples:
Visualization
def canAttendMeetings(intervals):
if not intervals:
return True
intervals.sort(key=lambda x: x[0])
for i in range(1, len(intervals)):
if intervals[i][0] < intervals[i-1][1]:
return False
return True
][42][129][96][1513

can attend meetings

0 / 5

Merging Intervals

When an interval overlaps with the previous interval in a list of intervals sorted by start times, they can be merged into a single interval.
To merge an interval into a previous interval, we set the end time of the previous interval to be the max of either end time.
prev_interval[1] = max(prev_interval[1], interval[1])
Python code for merging interval into prev_interval
In Merge Intervals, we are given a list of intervals and need to return a list with all overlapping intervals merged together. We create a new list containing the merged intervals, sort the intervals by their start times, and then iterate over each interval. If the current interval overlaps with the last interval in the merged list, we merge the current interval into the last interval in the merged list. Otherwise, we add the current interval to the merged list.
|
list of intervals [start, end]
Try these examples:
Visualization
def mergeIntervals(intervals):
sortedIntervals = sorted(intervals, key=lambda x: x[0])
merged = []
for interval in sortedIntervals:
if not merged or interval[0] > merged[-1][1]:
merged.append(interval)
else:
merged[-1][1] = max(interval[1], merged[-1][1])
return merged
][51][63][108][1815

merge intervals

0 / 10

Sorting by End Time

To see why we sometimes want to sort by end times instead of start time, let's consider the question of finding the maximum number of non-overlapping intervals in a given list of intervals.
Our solution will sort the intervals, and then greedily try to add each interval to the set of non-overlapping intervals.
If we sort by start time, we risk adding an interval that starts early but ends late, which will block us from adding other intervals until that interval ends.
For example, given the following intervals, if we sort by start time, choosing the first interval prevents us from adding another interval until after time 18. This blocks the remaining intervals from being added to the set of non-overlapping intervals, even though none of those intervals overlap with each other.
][64][1711][182][107
Sorting by start time yields 1 non-overlapping interval.
If instead we sort by end time, we can start by adding the intervals that end the earliest. Intuitively, this frees time for us to add more intervals as early as possible, and yields the correct answer.
][64][1711][182][107
Sorting by end time correctly yields 3 non-overlapping intervals.

Non-Overlapping Intervals

This is the basis for the question Non-Overlapping Intervals, in which we are given a list of intervals and asked to find the minimum number of intervals to remove to eliminate any overlap".
We sort the intervals by their end times, and then iterate over each interval, keeping a count of all intervals that DO NOT overlap with the last interval in the non-overlapping set. We return the total number of intervals minus the count of NON-overlapping intervals.
|
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

Practice Problems

Working through these questions will give you more practice with intervals:

Your account is free and you can post anonymously if you choose.