Given a sorted array of non-overlapping intervals intervals
, where each interval is represented as [start, end]
and sorted in ascending order by start
times, insert a new interval [start, end]
into the array. Ensure the resulting array remains sorted and free of overlapping intervals by merging any overlapping intervals as necessary.
intervals: Array<[number, number]>
: An array of integer pairsnewInterval: [number, number]
: An integer pair representing [start, end]
Input: intervals = [[1,2],[3,5],[6,10],[11,12],[13,16]], newInterval = [8,15]Output: [[1,2],[3,5],[6,16]]Explanation: The new interval [8, 15] overlaps with [6, 10], [11, 12], and [13, 16]. These intervals merge to form [6, 16], and the result is [[1, 2], [3, 5], [6, 16]].
Input: intervals = [[1,2],[3,4],[5,6],[7,8]], newInterval = [2,5]Output: [[1,6],[7,8]]Explanation: The new interval [2, 5] overlaps with [1, 2], [3, 4], and [5, 6]. Merging them results in [1, 6].
Input: intervals = [[2,4],[6,8],[10,12]], newInterval = [15,17]Output: [[2,4],[6,8],[10,12],[15,17]]Explanation: The new interval [15, 17] does not overlap with any existing intervals and is simply added at the end.
intervals.length
<= 100intervals[i].length == 2
start
<= end
<= 10,000Given a sorted array of non-overlapping intervals intervals
, where each interval is represented as [start, end]
and sorted in ascending order by start
times, insert a new interval [start, end]
into the array. Ensure the resulting array remains sorted and free of overlapping intervals by merging any overlapping intervals as necessary.
intervals: Array<[number, number]>
: An array of integer pairsnewInterval: [number, number]
: An integer pair representing [start, end]
Input: intervals = [[1,2],[3,5],[6,10],[11,12],[13,16]], newInterval = [8,15]Output: [[1,2],[3,5],[6,16]]Explanation: The new interval [8, 15] overlaps with [6, 10], [11, 12], and [13, 16]. These intervals merge to form [6, 16], and the result is [[1, 2], [3, 5], [6, 16]].
Input: intervals = [[1,2],[3,4],[5,6],[7,8]], newInterval = [2,5]Output: [[1,6],[7,8]]Explanation: The new interval [2, 5] overlaps with [1, 2], [3, 4], and [5, 6]. Merging them results in [1, 6].
Input: intervals = [[2,4],[6,8],[10,12]], newInterval = [15,17]Output: [[2,4],[6,8],[10,12],[15,17]]Explanation: The new interval [15, 17] does not overlap with any existing intervals and is simply added at the end.
intervals.length
<= 100intervals[i].length == 2
start
<= end
<= 10,000console.log()
statements will appear here.