Merge Intervals

MediumIntervals and Sweep Line

You are given an array of closed intervals in any order, where intervals[i] = [start, end] covers every point from start to end, both included. Two intervals overlap if they share at least one point: [1, 3] and [3, 5] overlap, [1, 2] and [3, 4] do not.

Merge every group of overlapping intervals into one interval covering exactly the same points, and return the resulting list of non-overlapping intervals. You may return them in any order.

Constraints

  • 1 <= intervals.length <= 10^4
  • 0 <= start <= end <= 10^4

Examples

Example 1

Input: intervals = [[1,4],[2,6],[8,10],[9,12]]
Output: [[1,6],[8,12]]

[1, 4] and [2, 6] overlap and become [1, 6]. [8, 10] and [9, 12] become [8, 12]. The two groups don't touch.

Example 2

Input: intervals = [[5,7],[1,3],[3,5]]
Output: [[1,7]]

The input isn't sorted. [1, 3] and [3, 5] share the point 3, which counts as overlapping, and [5, 7] shares 5 with [3, 5], so everything merges into [1, 7].

Example 3

Input: intervals = [[1,10],[2,3],[4,8]]
Output: [[1,10]]

[1, 10] already contains both of the other intervals.

JavaScript you'll use: Sorting and comparators Destructuring, spread and rest Arrays: create, read, update

/**
 * @param {number[][]} intervals
 * @return {number[][]}
 */
function merge(intervals) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.