</>

986. Interval List Intersections

Medium
Intervals Time O(M+N) Space O(M+N)
↗ Open on LeetCode Source: Misc

Quick review

Pattern
Intervals
Time
O(M+N)
Space
O(M+N)

Standard Solution (C++)

SummaryGiven two sorted lists of disjoint intervals, find all overlapping segments between the two lists and return them as a new list of intersection intervals.

Approach
  • Use two pointers, one per list, advancing through both simultaneously.
  • For each pair of intervals, an overlap exists when max(left endpoints) <= min(right endpoints); that overlap is exactly [max(left), min(right)].
  • Advance the pointer whose interval ends first, since it cannot overlap any later interval in the other list.
  • This greedy advance is safe because both input lists are already sorted and internally disjoint.
#include <vector>
using namespace std;

class Solution {
public:
    vector<vector<int>> intervalIntersection(vector<vector<int>>& firstList,
                                             vector<vector<int>>& secondList) {
        vector<vector<int>> result;
        int i = 0, j = 0;

        while (i < (int)firstList.size() && j < (int)secondList.size()) {
            // Overlap region, if any, is bounded by the later start and earlier end.
            int lo = max(firstList[i][0], secondList[j][0]);
            int hi = min(firstList[i][1], secondList[j][1]);

            if (lo <= hi)
                result.push_back({lo, hi});

            // The interval that ends sooner is exhausted; move past it.
            if (firstList[i][1] < secondList[j][1])
                ++i;
            else
                ++j;
        }

        return result;
    }
};
Time O(M + N) Space O(M + N)

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

My Solution (C++)

#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    vector<vector<int>> intervalIntersection(vector<vector<int>>& firstList, vector<vector<int>>& secondList) {
        int i=0, j=0;
        vector<vector<int>> res;

        while(i<firstList.size()&&j<secondList.size()){
            // check overlap
            int maxLeft=std::max(firstList[i][0], secondList[j][0]);
            int minRight=std::min(firstList[i][1], secondList[j][1]);
            if(maxLeft<=minRight){
                res.push_back({maxLeft, minRight});
            }
            if(firstList[i][1]<secondList[j][1]){
                ++i;
            }
            else{
                ++j;
            }
        }

        return res;
    }
};

// [[0,2],[5,10],[13,23],[24,25]] i=3
// [[1,5],[8,12],[15,24],[25,26]] j=3
// [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]

// Time Complexity: O(M+N), where M and N are the lengths of firstList and secondList, respectively.
// Space Complexity: O(M+N) for the result vector in the worst case when all intervals overlap.
Time O(M+N) Space O(M+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