Insert Interval
MediumP2Coding
intervals is a list of non-overlapping closed intervals [start, end], sorted by start. Insert newInterval and merge any intervals that overlap with it (intervals that share an endpoint count as overlapping). Return the resulting sorted list of non-overlapping intervals.
Example 1
- Input
- intervals = [[1,3],[6,9]], newInterval = [2,5]
- Output
- [[1,5],[6,9]]
Example 2
- Input
- intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
- Output
- [[1,2],[3,10],[12,16]]
- Why
- [4,8] overlaps [3,5], [6,7] and [8,10].
Constraints
- 0 <= intervals.length <= 10^4
- 0 <= start <= end <= 10^5
- intervals is sorted and non-overlapping
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 10. ⌘Enter runs, ⌘⇧Enter submits.