</>

57. Insert Interval

Medium NeetCode 150
Intervals Time O(n) Space O(n)
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
Intervals
Time
O(n)
Space
O(n)

Standard Solution (C++)

SummaryGiven a sorted, non-overlapping list of intervals and a new interval to insert, return the merged list where the new interval is placed correctly and any overlapping intervals are combined into one.

Approach
  • Three-phase linear scan: First, collect all intervals that end strictly before the new interval starts (no overlap, come before).
  • Merge overlapping: Expand the new interval by taking the min start and max end of any existing interval that overlaps with it.
  • Append remainder: After merging, push the (possibly expanded) new interval, then append all remaining intervals that start after it ends.
  • This works because the input is already sorted, so each phase can be done with a single forward pass in O(n) total.
#include <vector>
using namespace std;

class Solution {
public:
    vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
        vector<vector<int>> result;
        int n = intervals.size();
        int i = 0;

        // Phase 1: add all intervals that end before newInterval begins (no overlap)
        while (i < n && intervals[i][1] < newInterval[0]) {
            result.push_back(intervals[i++]);
        }

        // Phase 2: merge all overlapping intervals into newInterval
        while (i < n && intervals[i][0] <= newInterval[1]) {
            newInterval[0] = min(newInterval[0], intervals[i][0]);
            newInterval[1] = max(newInterval[1], intervals[i][1]);
            ++i;
        }
        result.push_back(newInterval);

        // Phase 3: add remaining intervals that start after newInterval ends
        while (i < n) {
            result.push_back(intervals[i++]);
        }

        return result;
    }
};
Time O(n) Space O(n)

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

class Solution {
public:
    vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
        vector<vector<int>> res;
        int n=intervals.size();
        int i=0;

        while(i<n && intervals[i][1]<newInterval[0]){
            res.push_back(intervals[i]);
            ++i;
        }

        while(i<n && intervals[i][0]<=newInterval[1]){
            newInterval[0]=std::min(newInterval[0], intervals[i][0]);
            newInterval[1]=std::max(newInterval[1], intervals[i][1]);
            ++i;
        }
        res.push_back(newInterval);
        
        while(i<n){
            res.push_back(intervals[i]);
            ++i;
        }

        return res;
    }
};

// Time Complexity: O(n)
// Space Complexity: O(n)
Time O(n) Space O(n)

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern