Insert Interval

Medium

intervals is sorted by start and has no overlaps. Insert newInterval, merging where needed, and return the still-sorted result.

Example 1

insertInterval()
Input
intervals = [[1,3],[6,9]], newInterval = [2,5]
Output
[[1,5],[6,9]]

Example 2

insertInterval()
Input
intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output
[[1,2],[3,10],[12,16]]
Constraints
0 <= intervals.length <= 10^4
intervals[i].length == 2
0 <= start <= end <= 10^5

Topics

ArraysIntervals

Implement insertInterval in the editor and return the answer — printing it is never graded.