You are given an array of non-overlapping intervals where each interval is represented as [start, end]. The intervals are already sorted by their start times in ascending order.
Your task is to insert a new interval into this array while maintaining the sorted order and ensuring no intervals overlap. If the new interval overlaps with existing intervals, you must merge them into a single interval.
Goal: Return the updated array of intervals after inserting the new interval.
Example: If you have intervals [[1,3],[6,9]] and want to insert [2,5], the result should be [[1,5],[6,9]] because [1,3] and [2,5] overlap and merge into [1,5].
Input & Output
Visualization
Time & Space Complexity
Single pass through all intervals, each interval processed once
O(n) space for the result array, no additional data structures needed
Constraints
- 0 โค intervals.length โค 104
- intervals[i].length == 2
- 0 โค starti โค endi โค 105
- intervals is sorted by starti in ascending order
- newInterval.length == 2
- 0 โค start โค end โค 105