dsa · medium

Merge Intervals

You are given intervals where intervals[i] = [start_i, end_i].

Arguments

Merge every overlapping interval and return a list of non-overlapping intervals that cover the input. Touching endpoints merge ([1,4] and [4,5] become [1,5]).

Example

[[1,3],[2,6],[8,10],[15,18]][[1,6],[8,10],[15,18]] because [1,3] and [2,6] overlap.

[[1,4],[4,5]][[1,5]].

Constraints

1 <= intervals.length <= 10^4 intervals[i].length == 2 0 <= start_i <= end_i <= 10^4 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.

Examples

Example 1

Input:
[[1,3],[2,6],[8,10],[15,18]]

Expected:
[[1,6],[8,10],[15,18]]

Example 2

Input:
[[1,4],[4,5]]

Expected:
[[1,5]]

Open in the Dojo editor