Merge Intervals
Problem statement
Each row [start, end] in intervals is a closed interval. Merge overlapping intervals until the result contains disjoint intervals covering exactly the same points.
Intervals that share an endpoint overlap. Return the merged intervals in ascending order of their starts.
Function
mergeIntervals(intervals: int[][]) → int[][]Examples
Example 1
intervals = [[1,3],[2,6],[8,10],[15,18]]return = [[1,6],[8,10],[15,18]]The first two intervals overlap and merge to [1,6].
Example 2
intervals = [[1,4],[4,5]]return = [[1,5]]The shared endpoint 4 makes these closed intervals overlap.
Example 3
intervals = [[4,7],[1,4]]return = [[1,7]]Sort the starts, then merge at the shared endpoint.
Constraints
1 <= intervals.length <= 10^4.- Each row has two entries with
0 <= start <= end <= 10^4.