You are given an array of non-overlapping intervals intervals where intervals[i] = [start_i, end_i] represents the start and the end time of the ith interval. intervals is initially sorted in ascending order by start_i.
You are given another interval newInterval = [start, end].
Insert newInterval into intervals such that intervals is still sorted in ascending order by start_i and also intervals still does not have any overlapping intervals. You may merge the overlapping intervals if needed.
Return intervals after adding newInterval.
Note: Intervals are non-overlapping if they have no common point. For example, [1,2] and [3,4] are non-overlapping, but [1,2] and [2,3] are overlapping.
Example 1:
Input: intervals = [[1,3],[4,6]], newInterval = [2,5]
Output: [[1,6]]Example 2:
Input: intervals = [[1,2],[3,5],[9,10]], newInterval = [6,7]
Output: [[1,2],[3,5],[6,7],[9,10]]Constraints:
0 <= intervals.length <= 1000newInterval.length == intervals[i].length == 20 <= start <= end <= 1000
You should aim for a solution with O(n) time and O(1) extra space, where n is the size of the input array.
The given intervals are non-overlapping and sorted in ascending order based on the start value. Try to visualize them as line segments and consider how a new interval can be inserted. Maybe you should analyze different cases of inserting a new interval.
First, we append all intervals to the output list that have an end value smaller than the start value of the new interval. Then, we encounter one of three cases: we have appended all intervals, we reach an interval whose start value is greater than the new interval’s end, or we find an overlapping interval. Can you think of a way to handle these cases efficiently?
We iterate through the remaining intervals, updating the new interval if its end value is greater than or equal to the current interval's start value. We adjust the start and end of the new interval to the minimum and maximum values, respectively. After this, any remaining intervals are appended to the output list, and we return the result.
Before attempting this problem, you should be comfortable with:
We are given a list of non-overlapping intervals sorted by start time, and we need to insert newInterval into the list while keeping the result sorted and non-overlapping.
Since the intervals are already sorted, we can process them in one pass and split the work into three simple parts:
Intervals completely before newInterval
Intervals that overlap with newInterval
newInterval:Intervals completely after the merged newInterval
This way, we only scan the list once and merge exactly when needed.
res and index i = 0.newInterval starts:intervals[i].end < newInterval.start, append intervals[i] to resnewInterval:intervals[i].start <= newInterval.end, update:newInterval.start = min(newInterval.start, intervals[i].start)newInterval.end = max(newInterval.end, intervals[i].end)newInterval to res.res.res.class Solution:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
n = len(intervals)
i = 0
res = []
while i < n and intervals[i][1] < newInterval[0]:
res.append(intervals[i])
i += 1
while i < n and newInterval[1] >= intervals[i][0]:
newInterval[0] = min(newInterval[0], intervals[i][0])
newInterval[1] = max(newInterval[1], intervals[i][1])
i += 1
res.append(newInterval)
while i < n:
res.append(intervals[i])
i += 1
return resWe are given a list of non-overlapping intervals sorted by start time, and we want to insert newInterval while keeping the final list sorted and merged.
A simple idea is:
newInterval should be inserted based on its start time.Binary search helps us avoid scanning from the beginning just to find the insertion position.
[newInterval].left where:intervals[left].start >= newInterval.startnewInterval should be insertednewInterval into intervals at index left.res.intervals:res is empty or the current interval starts after the last interval in res ends:resres[-1].end = max(res[-1].end, current.end)res.class Solution:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
if not intervals:
return [newInterval]
n = len(intervals)
target = newInterval[0]
left, right = 0, n - 1
while left <= right:
mid = (left + right) // 2
if intervals[mid][0] < target:
left = mid + 1
else:
right = mid - 1
intervals.insert(left, newInterval)
res = []
for interval in intervals:
if not res or res[-1][1] < interval[0]:
res.append(interval)
else:
res[-1][1] = max(res[-1][1], interval[1])
return resWe are inserting newInterval into a list of sorted, non-overlapping intervals and want the final result to remain sorted and non-overlapping.
A greedy approach works because as we scan from left to right, every interval falls into one of three cases relative to newInterval:
Completely after newInterval
newInterval ends before the current interval starts, there will be no overlap with any later interval either.newInterval here and return the answer immediately.Completely before newInterval
newInterval starts, it can be added to the result unchanged.Overlapping with newInterval
newInterval to cover both ranges.By continuously merging when needed and stopping early when newInterval is placed, we solve it in one pass.
res.intervals:newInterval ends before the current interval starts:newInterval to resres plus the remaining intervals (since everything after is already sorted and non-overlapping)newInterval starts after the current interval ends:res (it is safely before newInterval)newInterval:newInterval.start = min(newInterval.start, interval.start)newInterval.end = max(newInterval.end, interval.end)newInterval belongs at the end:newInterval to resresclass Solution:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
res = []
for i in range(len(intervals)):
if newInterval[1] < intervals[i][0]:
res.append(newInterval)
return res + intervals[i:]
elif newInterval[0] > intervals[i][1]:
res.append(intervals[i])
else:
newInterval = [
min(newInterval[0], intervals[i][0]),
max(newInterval[1], intervals[i][1]),
]
res.append(newInterval)
return resUsing the wrong condition to detect overlapping intervals leads to incorrect merging. Two intervals [a, b] and [c, d] overlap when a <= d AND c <= b. A common mistake is checking only one direction or using strict inequality (< instead of <=), which fails for adjacent intervals like [1, 2] and [2, 3] that should merge.
When the new interval belongs after all existing intervals (its start is greater than all existing ends), the merging loop never triggers and the new interval is never added. The algorithm must append the new interval to the result after the loop completes if it was not already inserted.
Some solutions modify the newInterval array during merging, which can cause issues if the caller expects the original input to remain unchanged. In languages where arrays are passed by reference, this side effect may lead to unexpected behavior in the calling code.