Skip to content
Onsite

Merge Intervals

MediumP1Coding

Given an array of intervals [start, end], merge all overlapping intervals and return the non-overlapping intervals that cover the same ranges. Return them sorted by start.

Two intervals overlap if they share at least one point, so [1,4] and [4,5] merge into [1,5]. The input is not sorted.

Example 1

Input
intervals = [[1,3],[2,6],[8,10],[15,18]]
Output
[[1,6],[8,10],[15,18]]

Example 2

Input
intervals = [[1,4],[4,5]]
Output
[[1,5]]

Example 3

Input
intervals = [[1,4]]
Output
[[1,4]]

Constraints

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

Hints

Run checks the 3 visible tests, Submit all 11.