Solution: Insert Interval
Understand how to insert a given interval into a sorted list of non-overlapping intervals and merge any overlapping intervals efficiently. Explore the step-by-step approach to maintaining sorted order without extra sorting, optimizing time complexity to linear. This lesson helps you implement the intervals pattern in coding interviews using an optimal one-pass algorithm.
Statement
You are given a list of non-overlapping intervals, intervals, where each interval is represented as [starti, endi] and the list is sorted in ascending order by the start of each interval (starti). You are also given another interval, new_interval = [start, end].
Your task is to insert new_interval into the list of intervals such that the list remains sorted by starting times and still contains no
Return the updated list of intervals.
Note: You don’t need to modify
intervalsin place. You can make a new array and return it.
Constraints:
intervals.lengthintervals[i].length,new_interval.length...