Advertisement

Insert Interval - LeetCode 57 Solution

Insert Interval - Complete Solution Guide

Insert Interval is LeetCode problem 57, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an array of non-overlapping intervals intervals where intervals[i] = [start i , end i ] represent the start and the end of the i th interval and intervals is sorted in ascending order by start i . You are also given an interval newInterval = [start, end] that represents the start and end of another interval. Insert newInterval into intervals such that intervals is still sorted in ascending order by start i and intervals still does not have any overlapping intervals (merge overlappi

Detailed Explanation

The problem requires inserting a new interval into a sorted list of non-overlapping intervals. The goal is to maintain the sorted order and ensure that no intervals overlap. If the new interval overlaps with existing intervals, they need to be merged to form a single, non-overlapping interval. The input is a list of intervals, where each interval is a list of two integers representing the start and end points, and a new interval to be inserted. The output is the modified list of intervals after the insertion and merging process.

Solution Approach

The solution uses a single pass through the existing sorted intervals list. It identifies and adds intervals that come before the new interval without overlapping. It then merges all overlapping intervals with the new interval, updating the new interval's start and end points accordingly. Finally, it adds the merged new interval and the remaining intervals that come after the merged interval.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty result list to store the final intervals.
  2. Step 2: Iterate through the input `intervals` list until we find an interval that either overlaps with `newInterval` or starts after `newInterval` ends.
  3. Step 3: For each interval encountered before overlapping, add it directly to the `result` list.
  4. Step 4: Once overlapping intervals are found, merge them by updating the `newInterval` start and end values. The start becomes the minimum of the current `newInterval` start and overlapping interval start. The end becomes the maximum of current `newInterval` end and overlapping interval end.
  5. Step 5: After merging all overlapping intervals, add the updated `newInterval` to the `result` list.
  6. Step 6: Add the remaining intervals (those after the merged section) to the `result` list.
  7. Step 7: Return the `result` list containing the merged intervals.

Key Insights

  • Insight 1: Since the intervals are sorted, we can iterate through them to find the correct position for the new interval and merge any overlapping intervals efficiently.
  • Insight 2: The key is to identify the intervals that come completely before the new interval, the intervals that overlap with the new interval (and need merging), and the intervals that come completely after the new interval.
  • Insight 3: We can achieve this in a single pass through the sorted list of intervals.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array.

Companies

Asked at: Adobe, Amazon, Apple, Bloomberg, Google, LinkedIn, Meta, Microsoft, MongoDB, Oracle, PhonePe, Tesco, TikTok, Uber, Yahoo.