Merge Intervals
Each item of intervals is [start, end]. Merge every group of intervals that overlap, and return the merged intervals sorted by start. Intervals that only touch, like [1, 3] and [3, 5], also merge.
The input may be in any order.
Example 1
intervals [[1,3],[2,6],[8,10],[15,18]]
returns [[1,6],[8,10],[15,18]]
[1, 3] and [2, 6] overlap and become [1, 6].
Example 2
intervals [[1,4],[4,5]]
returns [[1,5]]
They touch at 4, so the result is [[1, 5]].
Constraints
1 <= len(intervals) <= 200 <= start <= end <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.