LeetCode ยท #986

Interval List Intersections Solution

Given two sorted lists of disjoint intervals, return their intersection.

01
Section One ยท Problem

Problem Statement

๐Ÿท๏ธ

Difficulty

Medium

๐Ÿ”—

LeetCode

Problem #986

๐Ÿ—๏ธ

Pattern

Intervals โ€” two pointers

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 < m and j < 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
A=[[0,2],[5,10],...] B=[[1,5],[8,12],...]. Two pointers, advance smaller end. A[0]=[0,2], B[0]=[1,5]: lo=max(0,1)=1, hi=min(2,5)=2 โ†’ [1,2]. A ends first โ†’ i++. A[1]=[5,10], B[0]=[1,5]: lo=5, hi=5 โ†’ [5,5]. B ends first โ†’ j++. A[1]=[5,10], B[1]=[8,12]: lo=8, hi=10 โ†’ [8,10]. A ends first โ†’ i++. ...
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

ApproachTimeSpace
Brute forceO(mยทn)O(m+n)
Two pointers โ† optimalO(m+n)O(m+n) output
07
Section Seven ยท Edge Cases

Edge Cases & Pitfalls

CaseInputWhy It Matters
Empty listA=[], B=[[1,2]]No intersection. Return [].
No overlapA=[[1,2]], B=[[3,4]]lo=3 > hi=2. No intersection.
Single pointA=[[1,3]], B=[[3,5]]lo=3, hi=3 โ†’ [3,3] (single point interval).
One inside otherA=[[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.

โ† Back to Interval problems