Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals and return an array of the non-overlapping intervals that cover all the intervals in the input. Two intervals [a, b] and [c, d] overlap if a <= d and c <= b. For example, [[1,3],[2,6],[8,10],[15,18]] merges to [[1,6],[8,10],[15,18]].
Example 1
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: [1,3] and [2,6] overlap (2 ≤ 3), so they merge into [1,6]. [8,10] and [15,18] do not overlap anything, so they stay as they are.
Example 2
Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]
Explanation: [1,4] and [4,5] share the point 4, which counts as an overlap, so they merge into [1,5].
Example 3
Input: intervals = [[1,10]]
Output: [[1,10]]
Explanation: There is only one interval, so nothing needs merging.
Two intervals overlap iff one starts before the other ends. After sorting by start time, the only interval that can overlap the *current* one is the one we just placed in our output — a single comparison per step.
Run checks all cases above. Submit evaluates all test cases.