Introduction
This pattern describes an efficient technique to deal with overlapping intervals. In a lot of problems involving intervals, we either need to find overlapping intervals or merge intervals if they overlap.
Given two intervals (‘a’ and ‘b’), there will be six different ways the two intervals can relate to each other:
Understanding the above six cases will help us in solving all intervals related problems. Let’s jump onto our first problem to understand the Merge Interval pattern.
*Merge Intervals (medium)
Top Interview 150 | 56. Merge Intervals Similar | Top Interview 150 | 452. Minimum Number of Arrows to Burst Balloons Design Gurus Educative.io
Problem Statement
Given a list of intervals, merge all the overlapping intervals to produce a list that has only mutually exclusive intervals.
Example 1:
1 | Intervals: [[1,4], [2,5], [7,9]] |
Example 2:
1 | Intervals: [[6,7], [2,4], [5,9]] |
Example 3:
1 | Intervals: [[1,4], [2,6], [3,5]] |
onstraints:
- 1 <= intervals.length <= 10^4
intervals[i].length == 2
- 0 <= start_i <= end_i <= 10^4
Solution
Let’s take the example of two intervals (‘a’ and ‘b’) such that a.start <= b.start
. There are four possible scenarios:
Our goal is to merge the intervals whenever they overlap. For the above-mentioned three overlapping scenarios (2, 3, and 4), this is how we will merge them:
The diagram above clearly shows a merging approach. Our algorithm will look like this:
- Sort the intervals on the start time to ensure
a.start <= b.start
- If ‘a’ overlaps ‘b’ (i.e.
b.start <= a.end
), we need to merge them into a new interval ‘c’ such that:
1 | c.start = a.start |
- We will keep repeating the above two steps to merge ‘c’ with the next interval if it overlaps with ‘c’.
Code
Here is what our algorithm will look like:
1 | #class Interval: |
Time complexity
The time complexity of the above algorithm is O(N \ logN)*, where ‘N’ is the total number of intervals. We are iterating the intervals only once which will take O(N), in the beginning though, since we need to sort the intervals, our algorithm will take O(N \ logN)*.
Space complexity
The space complexity of the above algorithm will be O(N) as we need to return a list containing all the merged intervals. We will also need O(N) space for sorting. For Java, depending on its version, Collection.sort()
either uses Merge sort or Timsort, and both these algorithms need O(N) space. Overall, our algorithm has a space complexity of O(N).
Similar Problems
Problem 1: Given a set of intervals, find out if any two intervals overlap.
Example:
1 | Intervals: [[1,4], [2,5], [7,9]] |
Solution: We can follow the same approach as discussed above to find if any two intervals overlap.
*Insert Interval (medium)
Top Interview 150 |57. Insert Interval Design Gurus Educative.io
Problem Statement
Given a list of non-overlapping intervals sorted by their start time, insert a given interval at the correct position and merge all necessary intervals to produce a list that has only mutually exclusive intervals.
Example 1:
1 | Input: Intervals=[[1,3], [5,7], [8,12]], New Interval=[4,6] |
Example 2:
1 | Input: Intervals=[[1,3], [5,7], [8,12]], New Interval=[4,10] |
Example 3:
1 | Input: Intervals=[[2,3],[5,7]], New Interval=[1,4] |
Constraints:
- 1 <= intervals.length <= 10^4
intervals[i].length == 2
- 0 <= start_i <= end_i <= 10^5
intervals
is sorted by start_i inascending
order.newInterval.length == 2
- 0 <= start <= end <= 105
Solution
If the given list was not sorted, we could have simply appended the new interval to it and used the merge()
function from Merge Intervals. But since the given list is sorted, we should try to come up with a solution better than O(N \ logN)*
When inserting a new interval in a sorted list, we need to first find the correct index where the new interval can be placed. In other words, we need to skip all the intervals which end before the start of the new interval. So we can iterate through the given sorted listed of intervals and skip all the intervals with the following condition:
1 | intervals[i].end < newInterval.start |
Once we have found the correct place, we can follow an approach similar to Merge Intervals to insert and/or merge the new interval. Let’s call the new interval ‘a’ and the first interval with the above condition ‘b’. There are five possibilities:
The diagram above clearly shows the merging approach. To handle all four merging scenarios, we need to do something like this:
1 | c.start = min(a.start, b.start) |
Our overall algorithm will look like this:
- Skip all intervals which end before the start of the new interval, i.e., skip all
intervals
with the following condition:
1 | intervals[i].end < newInterval.start |
- Let’s call the last interval ‘b’ that does not satisfy the above condition. If ‘b’ overlaps with the new interval (a) (i.e.
b.start <= a.end
), we need to merge them into a new interval ‘c’:
1 | c.start = min(a.start, b.start) |
- We will repeat the above two steps to merge ‘c’ with the next overlapping interval.
Code
Here is what our algorithm will look like:
1 | #class Interval: |
Time complexity
As we are iterating through all the intervals only once, the time complexity of the above algorithm is O(N), where ‘N’ is the total number of intervals.
Space complexity
Ignoring the space needed for the result list, the algorithm runs in constant space O(1).
If we include the result list, the space complexity will be O(N) as we need to return a list containing all the merged intervals.
*Intervals Intersection (medium)
Top Interview 150 |986. Interval List Intersections Design Gurus Educative.io
Problem Statement
Given two lists of intervals, find the intersection of these two lists. Each list consists of disjoint intervals sorted on their start time.
Example 1:
1 | Input: arr1=[[1, 3], [5, 6], [7, 9]], arr2=[[2, 3], [5, 7]] |
Example 2:
1 | Input: arr1=[[1, 3], [5, 7], [9, 12]], arr2=[[5, 10]] |
Constraints:
0 <= arr1.length, arr2.length <= 1000
- arr1.length + arr2.length >= 1
- 0 <= start_i < end_i <= 10^9
- end_i < start_i+1
- 0 <= start_j < end_j <= 10^9
- end_j < start_j+1
Solution
This problem follows the Merge Intervals pattern. As we have discussed under Insert Intervals, there are five overlapping possibilities between two intervals ‘a’ and ‘b’. A close observation will tell us that whenever the two intervals overlap, one of the interval’s start time lies within the other interval. This rule can help us identify if any two intervals overlap or not.
Now, if we have found that the two intervals overlap, how can we find the overlapped part?
Again from the above diagram, the overlapping interval will be equal to:
1 | start = max(a.start, b.start) |
That is, the highest start time and the lowest end time will be the overlapping interval.
So our algorithm will be to iterate through both the lists together to see if any two intervals overlap. If two intervals overlap, we will insert the overlapped part into a result list and move on to the next interval which is finishing early.
Code
Here is what our algorithm will look like:
1 | #class Interval: |
Time complexity
As we are iterating through both the lists once, the time complexity of the above algorithm is O(N+M), where ‘N’ and ‘M’ are the total number of intervals in the input arrays respectively.
Space complexity
Ignoring the space needed for the result list, the algorithm runs in constant space O(1).
Conflicting Appointments (medium)
Design Gurus Educative.io
Problem Statement
Given an array of intervals representing ‘N’ appointments, find out if a person can attend all the appointments.
Example 1:
1 | Appointments: [[1,4], [2,5], [7,9]] |
Example 2:
1 | Appointments: [[6,7], [2,4], [8,12]] |
Example 3:
1 | Appointments: [[4,5], [2,3], [3,6]] |
Constraints:
- 1 <= intervals.length <= 10^4
intervals[i].length == 2
- 0 <= starti < endi <= 10^6
Solution
The problem follows the Merge Intervals pattern. We can sort all the intervals by start time and then check if any two intervals overlap. A person will not be able to attend all appointments if any two appointments overlap.
Code
Here is what our algorithm will look like:
1 | #class Interval: |
Time complexity
The time complexity of the above algorithm is O(N\logN)*, where ‘N’ is the total number of appointments. Though we are iterating the intervals only once, our algorithm will take O(N\logN)* since we need to sort them in the beginning.
Space complexity
The space complexity of the above algorithm will be O(N), which we need for sorting. For Java, Arrays.sort()
uses Timsort, which needs O(N) space.
Similar Problems
Problem 1: Given a list of appointments, find all the conflicting appointments.
Example:
1 | Appointments: [[4,5], [2,3], [3,6], [5,7], [7,8]] |
*Problem Challenge 1
Design Gurus Educative.io
Minimum Meeting Rooms (hard)
Given a list of intervals representing the start and end time of ‘N’ meetings, find the minimum number of rooms required to hold all the meetings.
Example 1:
1 | Meetings: [[1,4], [2,5], [7,9]] |
Example 2:
1 | Meetings: [[6,7], [2,4], [8,12]] |
Example 3:
1 | Meetings: [[1,4], [2,3], [3,6]] |
Example 4:
1 | Meetings: [[4,5], [2,3], [2,4], [3,5]] |
Solution
Let’s take the above-mentioned example (4) and try to follow our Merge Intervals approach:
Meetings: [[4,5], [2,3], [2,4], [3,5]]
Step 1: Sorting these meetings on their start time will give us: [[2,3], [2,4], [3,5], [4,5]]
Step 2: Merging overlapping meetings:
- [2,3] overlaps with [2,4], so after merging we’ll have => [[2,4], [3,5], [4,5]]
- [2,4] overlaps with [3,5], so after merging we’ll have => [[2,5], [4,5]]
- [2,5] overlaps [4,5], so after merging we’ll have => [2,5]
Since all the given meetings have merged into one big meeting ([2,5]), does this mean that they all are overlapping and we need a minimum of four rooms to hold these meetings? You might have already guessed that the answer is NO! As we can clearly see, some meetings are mutually exclusive. For example, [2,3] and [3,5] do not overlap and can happen in one room. So, to correctly solve our problem, we need to keep track of the mutual exclusiveness of the overlapping meetings.
Here is what our strategy will look like:
- We will sort the meetings based on start time.
- We will schedule the first meeting (let’s call it
m1
) in one room (let’s call itr1
). - If the next meeting
m2
is not overlapping withm1
, we can safely schedule it in the same roomr1
. - If the next meeting
m3
is overlapping withm2
we can’t user1
, so we will schedule it in another room (let’s call itr2
). - Now if the next meeting
m4
is overlapping withm3
, we need to see if the roomr1
has become free. For this, we need to keep track of the end time of the meeting happening in it. If the end time ofm2
is before the start time ofm4
, we can use that roomr1
, otherwise, we need to schedulem4
in another roomr3
.
We can conclude that we need to keep track of the ending time of all the meetings currently happening so that when we try to schedule a new meeting, we can see what meetings have already ended. We need to put this information in a data structure that can easily give us the smallest ending time. A Min Heap would fit our requirements best.
So our algorithm will look like this:
- Sort all the meetings on their start time.
- Create a min-heap to store all the active meetings. This min-heap will also be used to find the active meeting with the smallest end time.
- Iterate through all the meetings one by one to add them in the min-heap. Let’s say we are trying to schedule the meeting
m1
. - Since the min-heap contains all the active meetings, so before scheduling
m1
we can remove all meetings from the heap that have ended beforem1
, i.e., remove all meetings from the heap that have an end time smaller than or equal to the start time ofm1
. - Now add
m1
to the heap. - The heap will always have all the overlapping meetings, so we will need rooms for all of them. Keep a counter to remember the maximum size of the heap at any time which will be the minimum number of rooms needed.
Code
Here is what our algorithm will look like:
1 | # 没有class的版本,一样能过。没有定义class,所以在使用heap的时候吧list的两个元素反转了一下 |
Time complexity
The time complexity of the above algorithm is O(N\logN)*, where ‘N’ is the total number of meetings. This is due to the sorting that we did in the beginning. Also, while iterating the meetings we might need to poll/offer meeting to the priority queue. Each of these operations can take O(N\logN)*. Overall our algorithm will take O(N\logN)*.
Space complexity
The space complexity of the above algorithm will be O(N) which is required for sorting. Also, in the worst case scenario, we’ll have to insert all the meetings into the Min Heap (when all meetings overlap) which will also take O(N) space. The overall space complexity of our algorithm is O(N).
Similar Problems
Problem 1: Given a list of intervals, find the point where the maximum number of intervals overlap.
Problem 2: Given a list of intervals representing the arrival and departure times of trains to a train station, our goal is to find the minimum number of platforms required for the train station so that no train has to wait.
Both of these problems can be solved using the approach discussed above.
Problem Challenge 2
Design Gurus Educative.io
Maximum CPU Load (hard)
We are given a list of Jobs. Each job has a Start time, an End time, and a CPU load when it is running. Our goal is to find the maximum CPU load at any time if all the jobs are running on the same machine.
Example 1:
1 | Jobs: [[1,4,3], [2,5,4], [7,9,6]] |
Example 2:
1 | Jobs: [[6,7,10], [2,4,11], [8,12,15]] |
Example 3:
1 | Jobs: [[1,4,2], [2,4,1], [3,6,5]] |
Solution
The problem follows the Merge Intervals pattern and can easily be converted to Minimum Meeting Rooms. Similar to ‘Minimum Meeting Rooms’ where we were trying to find the maximum number of meetings happening at any time, for ‘Maximum CPU Load’ we need to find the maximum number of jobs running at any time. We will need to keep a running count of the maximum CPU load at any time to find the overall maximum load.
Code
Here is what our algorithm will look like:
1 | # 没有class的版本,一样能过。没有定义class,所以在使用heap的时候吧list的两个元素反转了一下 |
Time complexity
The time complexity of the above algorithm is O(N\logN)*, where ‘N’ is the total number of jobs. This is due to the sorting that we did in the beginning. Also, while iterating the jobs, we might need to poll/offer jobs to the priority queue. Each of these operations can take O(log\N)*. Overall our algorithm will take O(N\logN)*
Space complexity
The space complexity of the above algorithm will be O(N), which is required for sorting. Also, in the worst case, we have to insert all the jobs into the priority queue (when all jobs overlap) which will also take O(N) space. The overall space complexity of our algorithm is O(N)
**Problem Challenge 3
Leetcode 会员 Design Gurus Educative.io
Employee Free Time (hard)
For ‘K’ employees, we are given a list of intervals representing the working hours of each employee. Our goal is to find out if there is a free interval that is common to all employees. You can assume that each list of employee working hours is sorted on the start time.
Example 1:
1 | Input: Employee Working Hours=[[[1,3], [5,6]], [[2,3], [6,8]]] |
Example 2:
1 | Input: Employee Working Hours=[[[1,3], [9,12]], [[2,4]], [[6,8]]] |
Example 3:
1 | Input: Employee Working Hours=[[[1,3]], [[2,4]], [[3,5], [7,9]]] |
Solution
This problem follows the Merge Intervals pattern.
Let’s take the above-mentioned example (2) and visually draw it:
1 | Input: Employee Working Hours=[[[1,3], [9,12]], [[2,4]], [[6,8]]] |
One simple solution can be to put all employees’ working hours in a list and sort them on the start time. Then we can iterate through the list to find the gaps.(一开始我想的)
Let’s dig deeper.
Sorting the intervals of the above example will give us:
1 | [1,3], [2,4], [6,8], [9,12] |
We can now iterate through these intervals, and whenever we find non-overlapping intervals (e.g., [2,4] and [6,8]), we can calculate a free interval (e.g., [4,6]). This algorithm will take O(N \ logN)* time, where ‘N’ is the total number of intervals. This time is needed because we need to sort all the intervals. The space complexity will be O(N), which is needed for sorting. Can we find a better solution?
Using a Heap to Sort the Intervals
One fact that we are not utilizing is that each employee list is individually sorted!
How about we take the first interval of each employee and insert it in a Min Heap. This Min Heap can always give us the interval with the smallest start time. Once we have the smallest start-time interval, we can then compare it with the next smallest start-time interval (again from the Heap) to find the gap. This interval comparison is similar to what we suggested in the previous approach.
Whenever we take an interval out of the Min Heap, we can insert the next interval of the same employee. This also means that we need to know which interval belongs to which employee.
Code
Here is what our algorithm will look like:
大致可以看懂,但是定义EmplyeeInterval有点复杂了,可以直接使用三元组代替。
1 | from heapq import * |
Time complexity
The time complexity of the above algorithm is O(N\logK)*, where ‘N’ is the total number of intervals and ‘K’ is the total number of employees. This is due to the fact that we are iterating through the intervals only once (which will take O(N)), and every time we process an interval, we remove (and can insert) one interval in the Min Heap, (which will take O(N\logK)*). At any time the heap will not have more than ‘K’ elements.
Space complexity
The space complexity of the above algorithm will be O(K) as at any time the heap will not have more than ‘K’ elements.