Solution: Insert Interval
Let's solve the Insert Interval problem using the Merge Intervals pattern.
Statement#
Given a sorted list of nonoverlapping intervals and a new interval, your task is to insert the new interval into the correct position while ensuring that the resulting list of intervals remains sorted and nonoverlapping. Each interval is a pair of nonnegative numbers, the first being the start time and the second being the end time of the interval.
Constraints:
-
existing_intervals.length -
existing_intervals[i].length,new_interval.length== 2 -
start time end time
-
The list of intervals is sorted in ascending order based on the .
Solution#
So far, you’ve probably brainstormed some approaches and have an idea of how to solve this problem. Let’s explore some of these approaches and figure out which one to follow based on considerations such as time complexity and any implementation constraints.
Naive approach#
The naive approach involves iterating through the list of intervals and checking for overlaps with a new interval. If overlap occurs, the new interval merges with the existing one. After iterating the entire list, if no overlap occurs, append the new interval. Finally, we sort the list based on the start times of the intervals to ensure the correct order of intervals.
Iterating through the intervals to merge or append the new interval based on overlaps takes
Optimized approach using the merge intervals pattern#
The problem requires us to do two things. First, we need to insert a new interval into a sequence of nonoverlapping intervals. Second, the result should also be a list of nonoverlapping intervals. This requires merging the new interval with any overlapping interval(s) in the input. Therefore, the merge interval pattern applies.
1 of 7
2 of 7
3 of 7
4 of 7
5 of 7
6 of 7
7 of 7
Note: In the following section, we will gradually build the solution. Alternatively, you can skip straight to just the code.
Step-by-step solution construction#
First, we process the intervals that start before the new interval and then insert the new interval itself. Additionally, we need to merge the new interval with the existing intervals in the input.
Here’s how we’ll implement this feature:
Since the intervals are sorted, we'll create an empty list,
output, and start adding the intervals that start before the new interval.Next, we'll add the new interval to the
outputlist and merge it with the previously added interval if there is an overlap.Finally, we'll add the remaining intervals to the
outputlist, merging them with the previously added intervals when they overlap.
We’ll look at these steps and how they work individually to have a more comprehensive understanding of our solution. The first step is to see the new interval to be added and append all the intervals that start before it to a new list which we will use as our output later.
After appending the intervals that start before the new interval to the output list, we check if any intervals in the output list overlap with the new interval. If an overlap is found, we merge the intervals by updating the end time of the last interval in the output list to the maximum of its current end time and the end time of the new interval.
In this step, we handle the merging of intervals if there is an overlap with the new interval.
In this step, we handle the remaining intervals in the existing interval list after adding the new interval. We iterate through the remaining intervals and merge them with the intervals in the output list if there is an overlap. By comparing start and end times, we determine if an interval should be appended or merged. This process ensures that all overlapping intervals are correctly merged into the output list.
Finally, we obtain the final output list representing the merged intervals from the existing and new intervals.
Just the code#
Here’s the complete solution to this problem:
Solution summary#
- Append all intervals occurring before the new interval to the
outputlist until we find an interval that starts after the starting point of the new interval. - If there is an overlap between the last interval in the
outputlist and the new interval, merge them by updating the end value of the last interval. Otherwise, append the new interval to theoutputlist. - Continue iterating through the remaining intervals and merge the overlapping intervals with the last interval in the
outputlist. - Return the final
outputlist containing the merged intervals.
Time complexity#
The time complexity is , where is the number of intervals in the input list. This is because we iterate through the list once, checking and merging intervals as necessary.
Space complexity#
The space complexity is , since we only use constant space other than the input and output data structures.
Insert Interval
Employee Free Time