FunctionRacerProblemsRace

Insert Interval

MediumArraysPython, JavaScript, TypeScript, Java, C++

intervals is a list of [start, end] pairs, already sorted by start, that do not overlap each other. Add the pair newInterval to it.

If the new interval overlaps (or touches) any existing ones, merge them all into one. Return the resulting sorted list of intervals.

Example 1

intervals [[1,3],[6,9]]
newInterval [2,5]
returns [[1,5],[6,9]]

[2, 5] overlaps [1, 3], giving [1, 5]; [6, 9] stays.

Example 2

intervals []
newInterval [4,8]
returns [[4,8]]

Nothing to merge with.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Arrays problems