Insert Interval
You are given a set of non-overlapping intervals sorted by start, and a new interval. Insert the new interval so the result is still sorted and non-overlapping, merging with any intervals it overlaps.
Open official problem prompt ↗Splice one new range into an already-sorted, non-overlapping list while keeping both properties, merging where it touches existing ranges.
Like booking a new time block on a tidy calendar: appointments that end before it stay put, ones it collides with get folded into a single longer block, and later ones shift right unchanged.
- Input
- intervals = [[1,3],[6,9]], newInterval = [2,5]
- Output
- [[1,5],[6,9]]
- Why
- [2,5] overlaps [1,3] (2 <= 3) and merges into [1,5]; [6,9] starts after 5 so it stays separate.
0 <= intervals.length <= 10^4intervals[i].length == 20 <= start_i <= end_i <= 10^5intervals is sorted by start_i in ascending ordernewInterval.length == 20 <= start <= end <= 10^5