![]() |
VOOZH | about |
Given a set of non-overlapping intervals[][] where intervals[i] = [starti , endi] represent the start and the end of the ith event and intervals is sorted in ascending order by starti and a new interval, insert the interval at the correct position such that after insertion, the intervals remain sorted. If the insertion results in overlapping intervals, then merge the overlapping intervals. Assume that the set of non-overlapping intervals is sorted based on start time.
Examples:
Input: intervals[][] = [[1, 3], [4, 5], [6, 7], [8, 10]], newInterval[] = [5, 6]
Output: [[1, 3], [4, 7], [8, 10]]
Explanation: The intervals [4, 5] and [6, 7] are overlapping with [5, 6]. So, they are merged into one interval [4, 7].Input: intervals[][] = [[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]], newInterval[] = [4, 9]
Output: [[1, 2], [3, 10], [12, 16]]
Explanation: The intervals [ [3, 5], [6, 7], [8, 10] ] are overlapping with [4, 9]. So, they are merged into one interval [3, 10].
Table of Content
The approach is to append the new interval to the given array of intervals and then handle the overlapping of intervals. So, we will use the same approach as Merge Overlapping Intervals to merge the overlapping intervals after insertion.
1 3 4 7 8 10
When we add a new interval, it may overlap with some contiguous intervals in the array. The overlapping intervals can be found in a contiguous subarray because the intervals array is already sorted. To remove overlapping we find these overlapping interval's subarray and merge them with new interval, to form a single merged interval.
Now to maintain the order sorted, we first add the lower intervals, then this merged interval, and finally the remaining intervals in the result.
1 3 4 7 8 10