Skip to content
BASA DSA

Merge Intervals

MediumIntervals

Merge overlapping closed intervals and return their union as sorted disjoint closed intervals. Touching endpoints overlap.

Example 1

Input
intervals = [[5,8],[1,3],[3,6]]
Output
[[1,8]]

Explanation

[1,3] touches [3,6], which overlaps [5,8], merging the chain into [1,8].

Example 2

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

Explanation

The gap between 2 and 4 keeps the intervals separate.

Example 3

Input
intervals = [[2,3],[1,9]]
Output
[[1,9]]

Explanation

[2,3] is contained in [1,9] and adds no endpoints.

Constraints

  • 1 <= number of intervals <= 10000
  • Each pair has start <= end

Function contractmergeArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…