Search⌘ K
AI Features

Solution: Insert Interval

Explore how to insert a new interval into a sorted array of non-overlapping intervals, merging any that overlap to maintain order without sorting. Understand the step-by-step technique, including appending intervals preceding the new interval, merging overlaps, and efficiently handling the rest. This lesson helps you develop an optimized linear time approach essential for interval-based coding interview problems.

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, newInterval = [start, end].

Your task is to insert newInterval into the list of intervals such that the list remains sorted by starting times and still contains no overlapping intervalsOverlapping intervals are two or more intervals with at least one common point in time.. If any intervals overlap after the insertion, merge them accordingly.

Return the updated list of intervals.

Note: You don’t need to modify intervals in place. You can make a new array and return it.

Constraints:

    ...