Insert Interval
intervals is a list of [start, end] pairs, already sorted by start, that do not overlap each other. Add the pair newInterval to it.
If the new interval overlaps (or touches) any existing ones, merge them all into one. Return the resulting sorted list of intervals.
Example 1
intervals [[1,3],[6,9]]
newInterval [2,5]
returns [[1,5],[6,9]]
[2, 5] overlaps [1, 3], giving [1, 5]; [6, 9] stays.
Example 2
intervals []
newInterval [4,8]
returns [[4,8]]
Nothing to merge with.
Constraints
0 <= len(intervals) <= 200 <= start <= end <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Arrays problems
- Longest HillMedium
- Maximum SubarrayMedium
- Next PermutationMedium
- Nudge Every ItemMedium
- Product of Array Except SelfMedium
- Range IncrementsMedium
- Rising TripleMedium
- Shortest Span to SortMedium