To solve this problem, we need to insert a new interval into a set of non-overlapping, sorted intervals and merge any overlapping intervals if necessary. The solution involves three main steps: adding intervals that come before the new interval, merging overlapping intervals with the new interval, and adding the remaining intervals that come after the merged interval.
Approach
- Add Non-overlapping Intervals Before: First, we add all intervals that end before the start of the new interval. These intervals do not overlap with the new interval and can be directly included in the result.
- Merge Overlapping Intervals: Next, we merge the new interval with all intervals that overlap with it. For each overlapping interval, we update the start of the merged interval to the minimum of the current merged start and the interval's start, and the end to the maximum of the current merged end and the interval's end.
- Add Non-overlapping Intervals After: Finally, we add all remaining intervals that start after the end of the merged interval. These intervals do not overlap with the merged interval and can be directly included in the result.
Solution Code
def insert(intervals, newInterval):
result = []
i = 0
n = len(intervals)
new_start, new_end = newInterval[0], newInterval[1]
# Add all intervals before the new interval
while i < n and intervals[i][1] < new_start:
result.append(intervals[i])
i += 1
# Merge overlapping intervals with newInterval
while i < n and intervals[i][0] <= new_end:
new_start = min(new_start, intervals[i][0])
new_end = max(new_end, intervals[i][1])
i += 1
# Add the merged interval
result.append([new_start, new_end])
# Add remaining intervals after the merged interval
while i < n:
result.append(intervals[i])
i += 1
return result
Explanation
- Non-overlapping Before: We iterate through the intervals and add any interval that ends before the start of the new interval. This ensures we only process intervals that could potentially overlap with the new interval next.
- Merge Overlapping: For each interval that starts before or at the end of the current merged interval, we adjust the merged interval's start and end to include this interval. This step handles all overlaps with the new interval.
- Non-overlapping After: After merging, we add all remaining intervals that start after the merged interval ends, as these do not overlap with the merged interval.
This approach efficiently processes the intervals in linear time, O(n), where n is the number of intervals, making it optimal for this problem. The solution maintains the sorted order of intervals as required.
Example 1:
Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
Output: [[1,5],[6,9]]
Example 2:
Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output: [[1,2],[3,10],[12,16]]
These examples demonstrate the solution's ability to handle both partial and full overlaps, as well as adjacent intervals. The code correctly merges the intervals and returns the result in sorted order.


作者声明:本文包含人工智能生成内容。