MediumIntervals · Sorting

Merge Intervals

Given an array of intervals (in any order), merge all overlapping intervals and return the non-overlapping intervals that cover them. Intervals that share a point, like [1,2] and [2,3], overlap.

[[1,3],[1,5],[6,7]] → [[1,5],[6,7]][[1,2],[2,3]] → [[1,3]]

Try:
CALL
1 / 1

Code

The highlighted line runs next. The pill shows the live values in the running call.

Variables

The data structures the code is building

Number line

One row per input interval; merged blocks below

Time and space complexity

n = len(intervals). The highlighted row is the solution being traced.

Keyboard: ← → step, Space play or pause. 1 to 8 intervals with values from 0 to 40, written as JSON.