...

/

Insert Interval

Insert Interval

Try to solve the Insert Interval problem.

Statement

Given a sorted list of nonoverlapping intervals and a new interval, your task is to insert the new interval into the correct position while ensuring that the resulting list of intervals remains sorted and nonoverlapping. Each interval is a pair of nonnegative numbers, the first being the start time and the second being the end time of the interval.

Constraints:

  • 0≤0 \leq existing_intervals.length ≤104\leq 10^4

  • existing_intervals[i].length, new_interval.length == 2

  • 0≤0 \leq start time << end time ≤104\leq 10^4

  • The list of intervals is sorted in ascending order based on the start timestart ~time.

Examples

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps us to check if you’re solving the correct problem:

Insert Interval

1

What will be the updated list of intervals?

existing_intervals = [[1,3],[4,5],[7,8],[9,12],[13,14]][[1, 3], [4, 5], [7, 8], [9, 12], [13, 14]]

new_interval = [2,10][2, 10]

A)

[[1,12],[13,14]][[1, 12], [13, 14]]

B)

[[1,3],[2,10],[4,5],[7,8],[9,12],[13,14]][[1, 3], [2, 10], [4, 5], [7, 8], [9, 12], [13, 14]]

C)

[[2,10],[13,14]][[2, 10], [13, 14]]

Question 1 of 40 attempted

Figure it out

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5
6

Try it yourself

Implement your solution in InsertInterval.cpp in the following coding playground.

Press + to interact
C++
usercode > InsertInterval.cpp
vector<vector<int>> InsertInterval(vector<vector<int>> existingIntervals, vector<int> newInterval)
{
// Replace this placeholder return statement with your code
return {};
}
Insert Interval

Access this course and 1200+ top-rated courses and projects.