Insert Interval
To solve this coding challenge, we need to insert a new interval into an existing array of non-overlapping intervals while making sure that the merged result is still a set of non-overlapping intervals, sorted by their start times. Let's break down the steps comprehensively.
, which efficiently handles even the upper constraint limit of the intervals list size.
Explanation
Goal
Given an array of non-overlapping intervals sorted in ascending order by their start times, and a new interval, we want to insert this new interval into the array while maintaining the order and ensuring no overlapping exists. The solution has to:- Traverse the existing intervals.
- Determine where the new interval can fit without causing any overlaps.
- Merge intervals if there are any overlaps.
- Add all intervals (including the merged new interval) to the result array and return it.
- Initialization and Result Preparation:
- Prepare an empty list to store the resulting intervals after insertion.
- Use a pointer to traverse through the given intervals.
- First Part - Before New Interval:
- First, we need to add all intervals that end before the new interval starts. These intervals don't overlap with the new interval and can be added directly to the output list.
- Second Part - Merging Overlapping Intervals:
- For intervals that overlap with the new interval, we merge them. The merging is done by comparing start and end times to ensure the entire span of overlap is captured within a single interval.
- Third Part - After New Interval:
- Add the remaining intervals that start after the new interval ends. These intervals also don't overlap with the new interval and can be added directly to the output list.
- Return the Result List:
- Finally, the result list which contains the merged intervals is returned.
-
Initialize an empty list
result
-
Initialize pointer
i
- Add intervals before newInterval without overlapping:
-
While
i
i
newInterval
i
result
i
- Merge overlapping intervals with newInterval:
-
While
i
i
newInterval
newInterval
newInterval
i
- Add newInterval after merging overlaps:
-
Add
newInterval
result
- Add remaining intervals after newInterval without overlapping:
-
While
i
i
result
i
-
Return the
result
Strategy
Detailed Steps in Pseudocode
Pseudocode
# Initialize result list
result = []
# Initialize index pointer i to 0
i = 0
# Step 1: Add all intervals before newInterval
while i < length of intervals AND intervals[i][1] < newInterval[0]:
add intervals[i] to result
increment i
# Step 2: Merge overlapping intervals with newInterval
while i < length of intervals AND intervals[i][0] <= newInterval[1]:
newInterval[0] = min(newInterval[0], intervals[i][0])
newInterval[1] = max(newInterval[1], intervals[i][1])
increment i
# Add the merged newInterval to result
add newInterval to result
# Step 3: Add all remaining intervals after newInterval
while i < length of intervals:
add intervals[i] to result
increment i
# Return the result list containing merged intervals
return result
Comprehensive Walkthrough
-
Initialize
result
i
-
Traverse the intervals and add non-overlapping intervals to the
result
- Merge overlapping intervals with the new one.
-
Add the merged interval into the
result
- Append rest intervals which come after the merged interval.
O(n)