InferenceAI

Enter at least two characters to search.

Start free

Insert an Interval

mediumGreedy & Intervals25 min25 XP

intervals is sorted by start and non-overlapping. Insert new and merge any overlaps (intervals that touch, like [1, 2] and [2, 3], merge). Return the result sorted by start.

Examples

EXAMPLE WALKTHROUGHmerge one

A guided view of the published example, not a trace of your code.

INPUT[[[1,3],[6,9]],[2,5]]
TASKinsert_an_interval()
EXPECTED OUTPUT[[1,5],[6,9]]

Start with the input values shown in this example.

Example 1 of 3

Input: intervals = [[1,3],[6,9]], new = [2,5]

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

Explanation: merge one

Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], new = [4,8]

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

Explanation: merge several

Input: intervals = [], new = [5,7]

Output: [[5,7]]

Explanation: empty list

Hint 1

Copy intervals that end before new starts, merge the ones that overlap, then copy the rest.

Topics

DSAGreedy & IntervalsPython