MediumIntervals · Array

Insert Interval

intervals is sorted by start and has no overlaps. Insert newInterval so the list stays sorted and overlap-free, merging where needed. Intervals that share a point, like [1,2] and [2,3], overlap.

[[1,3],[4,6]] + [2,5] → [[1,6]][[1,2],[3,5],[9,10]] + [6,7] → [[1,2],[3,5],[6,7],[9,10]]

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

Original intervals, newInterval, and the result

Time and space complexity

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

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