Interval List Intersections

Intersect two lists of disjoint sorted intervals — merge-walk with two pointers.

Medium
Time O(n + m) Space O(n + m)
LeetCode
3 min read
intervals two-pointers

Problem#

Given two lists of pairwise-disjoint, sorted-by-start intervals, return their intersection.

Examples#

  • A = [[0,2],[5,10]], B = [[1,5],[8,12]][[1,2],[5,5],[8,10]]

Constraints#

  • 0 <= A.length, B.length <= 1000.

Approach#

Two-pointer merge walk. The intersection of A[i] and B[j] is [max(a0,b0), min(a1,b1)] if non-empty. Advance the pointer with the smaller end.

Solution#

Interval List Intersections
class Solution {
public:
vector<vector<int>> intervalIntersection(vector<vector<int>>& A, vector<vector<int>>& B) {
vector<vector<int>> ans;
int i = 0, j = 0;
while (i < (int)A.size() && j < (int)B.size()) {
int lo = max(A[i][0], B[j][0]);
int hi = min(A[i][1], B[j][1]);
if (lo <= hi) ans.push_back({lo, hi});
if (A[i][1] < B[j][1]) ++i; else ++j;
}
return ans;
}
};

Editorial#

Advancing the pointer with the smaller endpoint is the right rule because anything to the right of the other endpoint can still intersect with a later interval on this side. Each pointer advances at most n + m times → linear.

Complexity#

  • Time: O(n + m).
  • Space: O(n + m).

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.