CodingMedium
Merge Intervals
You’re given an array intervals, where intervals[i] = [start, end]. Merge every interval that overlaps another, and return the intervals that remain, sorted by start.
Two intervals overlap when one starts at or before the moment the other ends, so [1, 4] and [4, 5] merge into [1, 5].
Example 1
Input
intervals = [[1,3],[2,6],[8,10],[15,18]]Output[[1,6],[8,10],[15,18]][1,3] and [2,6] overlap, so they merge into [1,6].
Example 2
Input
intervals = [[1,4],[4,5]]Output[[1,5]]Touching ends count as an overlap.
Constraints
- 1 ≤ intervals.length ≤ 10⁴
- intervals[i].length = 2
- 0 ≤ start ≤ end ≤ 10⁴