Medium · Intervals

Insert interval

Given a list of closed intervals sorted by start and pairwise non-overlapping, and a new interval x, insert x and merge it with every interval it overlaps (touching endpoints count), returning the list still sorted and non-overlapping.

Examples

Example 1

[[1,3],[6,9]] + [2,5]

Output: [1,5] [6,9]

Example 2

[[1,2],[3,5],[6,7],[8,10],[12,16]] + [4,8]

Output: [1,2] [3,10] [12,16]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Intervals problems