Merge Overlapping Intervals
Problem statement
Given an array intervals, where intervals[i] = [start, end] is a closed interval, merge every pair of intervals that overlaps.
Return the non-overlapping merged intervals sorted by ascending start value.
Intervals that share an endpoint overlap. For example, [1, 4] and [4, 6] merge into [1, 6].
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 become [1, 6]. The other two intervals stay separate.
Example 2
intervals = [[1,4],[4,5]]return = [[1,5]]The intervals share endpoint 4, so they overlap under the closed-interval rule.
Example 3
intervals = [[5,7],[1,10],[2,3]]return = [[1,10]]The interval [1, 10] contains both other intervals, even though the input is not sorted.
Constraints
1 <= intervals.length <= 10^5.intervals[i].length == 2.-10^9 <= intervals[i][0] <= intervals[i][1] <= 10^9.