← DiffPush

Insert Interval

Standard Bar Greedy Approach · Medium O(n) · O(n)

The Maintenance Window Merger

A datacenter keeps an ordered list of non-overlapping maintenance windows. Ops files one new window that may overlap several existing ones. The calendar tool walks the sorted list: it copies every window that ends before the new one starts, absorbs every window it collides with by stretching the new one, and finally slots the (possibly enlarged) window before whatever remains.

Input: A sorted non-overlapping list of intervals and a newInterval [start, end].

Output: Return the merged, still-sorted, non-overlapping interval list.

Constraints

Examples

Example 1

Input: {"intervals":[[1,2],[3,5],[6,7],[8,10],[12,16]],"newInterval":[4,8]}
Output: [[1,2],[3,10],[12,16]]
[4,8] swallows [3,5], [6,7] and [8,10] into one stretched window.

Example 2

Input: {"intervals":[[1,3],[6,9]],"newInterval":[2,5]}
Output: [[1,5],[6,9]]
[2,5] merges with [1,3] and stays clear of [6,9].

Solve this in your browser →

Also on LeetCode ↗