LeetCode ยท #986
Interval List Intersections Solution
Given two sorted lists of disjoint intervals, return their intersection.
01
Section One ยท Problem
Problem Statement
Given two lists of closed intervals, each sorted and disjoint, return the intersection of these two interval lists.
Example
Input: A = [[0,2],[5,10],[13,23],[24,25]]
B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Constraints
โข 0 โค firstList.length, secondList.length โค 1000 โข Each list is sorted and disjoint
02
Section Two ยท Approach 1
Brute Force โ O(mยทn)
For each interval in A, check every interval in B for intersection.
Problem: O(mยทn). Since both lists are sorted, use two pointers โ advance the one that ends earlier. O(m+n).
03
Section Three ยท Approach 2
Two Pointers โ O(m+n)
Pointer i for A, j for B. At each step, compute the intersection [max(start), min(end)]. If valid (start โค end), add it. Advance the pointer whose interval ends first (it can't intersect anything else from the other list).
๐ก Mental model: Two timelines laid side by side. Two fingers walk along each. They find where the timelines overlap (max of starts, min of ends). Then the finger on the timeline that ends first moves to the next interval โ the other timeline's interval might still overlap the next one.
Algorithm
i = 0,j = 0.- While
i < mandj < n: lo = max(A[i].start, B[j].start),hi = min(A[i].end, B[j].end).- If
lo โค hi: add[lo, hi]. - If
A[i].end < B[j].end:i++. Else:j++.
๐ฏ When to recognize this pattern: "Intersect two sorted interval lists." Two pointers on sorted sequences โ standard merge-like technique. The key decision: advance the pointer with the smaller end.
04
Section Four ยท Trace
Visual Walkthrough
Interval Intersections โ Two Pointers
05
Section Five ยท Implementation
Code โ Java & Python
Java โ Two pointers
import java.util.*;
class Solution {
public int[][] intervalIntersection(int[][] A, int[][] B) {
List<int[]> res = new ArrayList<>();
int i = 0, j = 0;
while (i < A.length && j < B.length) {
int lo = Math.max(A[i][0], B[j][0]);
int hi = Math.min(A[i][1], B[j][1]);
if (lo <= hi) res.add(new int[]{lo, hi});
if (A[i][1] < B[j][1]) i++;
else j++;
}
return res.toArray(new int[res.size()][]);
}
}
Python โ Two pointers
class Solution:
def intervalIntersection(self, A: list[list[int]], B: list[list[int]]) -> list[list[int]]:
res, i, j = [], 0, 0 while i < len(A) and j < len(B):
lo = max(A[i][0], B[j][0])
hi = min(A[i][1], B[j][1])
if lo <= hi: res.append([lo, hi])
if A[i][1] < B[j][1]: i += 1 else: j += 1 return res
06
Section Six ยท Analysis
Complexity Analysis
| Approach | Time | Space |
|---|---|---|
| Brute force | O(mยทn) | O(m+n) |
| Two pointers โ optimal | O(m+n) | O(m+n) output |
07
Section Seven ยท Edge Cases
Edge Cases & Pitfalls
| Case | Input | Why It Matters |
|---|---|---|
| Empty list | A=[], B=[[1,2]] | No intersection. Return []. |
| No overlap | A=[[1,2]], B=[[3,4]] | lo=3 > hi=2. No intersection. |
| Single point | A=[[1,3]], B=[[3,5]] | lo=3, hi=3 โ [3,3] (single point interval). |
| One inside other | A=[[1,10]], B=[[3,5]] | Intersection = [3,5]. |
โ Common Mistake: Advancing both pointers when intervals have the same end. This can miss intersections. Only advance the one with the smaller end. If ends are equal, advancing either works (both are exhausted for future overlaps), but advancing only one is safer and simpler.