Solution: Insert Interval

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:

  • 00 \leq existing_intervals.length 104\leq 10^4

  • existing_intervals[i].length, new_interval.length == 2

  • 00 \leq start time << end time 104\leq 10^4

  • The list of intervals is sorted in ascending order based on the start timestart ~time.

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 O(n)O(n) time, where nn represents the number of intervals. Then, sorting the output list takes O(nlogn)O(n \log n) time. Therefore, the overall time complexity of this solution is O(nlogn)O(n \log n) due to sorting being the dominant factor. 

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.

Created with Fabric.js 3.6.6 Intervals [1, 3] [5, 7] [9, 10] [6, 8] Intervalto be added UpdatedIntervals We've created a new output array that will contain thecontent of our updated list.

1 of 7

Created with Fabric.js 3.6.6 [6, 8] Intervals Intervalto be added UpdatedIntervals [1, 3] [5, 7] [9, 10] [1, 3] We add [1, 3] to the output array, since its starting pointis less than the starting point of the interval to be added.

2 of 7

Created with Fabric.js 3.6.6 Intervalto be added UpdatedIntervals Intervals [1, 3] [5, 7] [9, 10] [6, 8] [1, 3] We verify that the current interval [1, 3] ends before the start of theinterval to be added.

3 of 7

Created with Fabric.js 3.6.6 Intervalto be added Intervals UpdatedIntervals [1, 3] [5, 7] [9, 10] [6, 8] [1, 3] [5, 7] We add [5, 7] to the output array, since its starting pointis less than the starting point of the interval to be added.

4 of 7

Created with Fabric.js 3.6.6 [1, 3] [5, 7] [9, 10] [6, 8] Intervals Intervalto be added UpdatedIntervals [1, 3] [5, 7] We verify that the the current interval does not end before the startof the interval to be added.

5 of 7

Created with Fabric.js 3.6.6 Intervals Intervalto be added [1, 3] [5, 7] [9, 10] UpdatedIntervals [1, 3] [5, 8] We merge the intervals to update our list.

6 of 7

Created with Fabric.js 3.6.6 Intervals Intervalto be added [1, 3] [5, 7] [9, 10] UpdatedIntervals [1, 3] [5, 8] [9, 10] Finally, we will add the remaining interval in the output list.

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 output list and merge it with the previously added interval if there is an overlap.

  • Finally, we'll add the remaining intervals to the output list, 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.

Insert Interval

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.

Insert 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.

Insert Interval
Just the code#

Here’s the complete solution to this problem:

Insert Interval
Solution summary#
  • Append all intervals occurring before the new interval to the output list 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 output list and the new interval, merge them by updating the end value of the last interval. Otherwise, append the new interval to the output list.
  • Continue iterating through the remaining intervals and merge the overlapping intervals with the last interval in the output list.
  • Return the final output list containing the merged intervals.
Time complexity#

The time complexity is O(n)O(n), where nn 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 O(1)O(1), since we only use constant space other than the input and output data structures.

Insert Interval

Employee Free Time