Insert Interval
Try to solve the Insert Interval problem.
We'll cover the following
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:
-
existing_intervals.length
-
existing_intervals[i].length
,new_interval.length
== 2 -
start time end time
-
The list of intervals is sorted in ascending order based on the .
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
What will be the updated list of intervals?
existing_intervals =
new_interval =
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.
Try it yourself
Implement your solution in the following coding playground.
import java.util.*;class Solution {public static int[][] insertInterval(int[][] existingIntervals, int[] newInterval) {// Replace this placeholder return statement with your codereturn new int[][]{};}}