Interval List Intersections
Interval List Intersections is LeetCode 986 (Medium). Two lists of closed intervals are given. Inside each list the intervals are sorted and never touch each other, but an interval from one list can overlap any number of intervals from the other. Return every stretch of numbers covered by both lists, in order.
- Closed means both ends belong to the interval, so
[2, 5]and[5, 8]share the single point[5, 5]. - Either list can be empty, which leaves nothing in common.
- 0 <= firstList.length, secondList.length <= 1000
- firstList.length + secondList.length >= 1
- 0 <= startᵢ < endᵢ <= 10⁹, and endᵢ < startᵢ₊₁ in each list
- 0 <= startⱼ < endⱼ <= 10⁹, and endⱼ < startⱼ₊₁ in each list
Intuition
For one pair of intervals, the common part is easy: it begins at the later of the two starts and stops at the earlier of the two ends. If that begin point is past that stop point, the pair shares nothing.
The real question is which pair to look at next. Compare the two ends. The interval that finishes first cannot meet anything further along the other list, because every later interval there starts after the current one, which already reaches at least as far. So drop that interval and keep the other, which may still overlap the next one. Each comparison throws one interval away, so the two lists are read side by side, once.
Two sorted lists that must be read together, where each step can safely discard the item from one side, is the merge-style two-pointer pattern.
Approach
Before reading on, take [[1, 7]] and [[3, 4], [7, 9]]. Which pairs share numbers, and what is shared? After comparing [1, 7] with [3, 4], which of the two can never matter again?
Point at the first interval of each list
Set i = 0 in the first list and j = 0 in the second. Every pair that could overlap is met by moving these two indexes forward, so there is no nested loop.
Work out what the current pair shares
Take start = max of the two starts and end = min of the two ends. If start <= end, record [start, end]. The test uses <= because closed intervals that meet at one number share that number.
Move past the interval that ends first
If the first list's interval ends sooner, step i; otherwise step j. The one that ends first has given away everything it can, while the other may still reach into the next interval on the opposite side.
Stop when either list is used up
Once one list has no intervals left, nothing remaining in the other list has a partner, so the loop ends and the recorded pieces are the answer, already in order.
Code in Python, C++ and Java
Common pitfalls
Dropping one-point overlaps
if start < end:
if start <= end:
The intervals are closed, so a shared endpoint is a real overlap. On [[1, 5]] and [[5, 9]] the answer is [[5, 5]]; the strict test returns [].
Moving both indexes after a match
result.append([start, end]) i += 1 j += 1
if firstList[i][1] < secondList[j][1]:
i += 1
else:
j += 1One long interval can overlap several short ones. On [[1, 7]] and [[3, 4], [7, 9]], stepping both after [3, 4] ends the first list and loses [7, 7].
Edge cases
[] and [[1, 3]]The loop condition fails at once and the answer is [].
[[2, 6]] and [[4, 6]]Either index may move; the code steps j. The other interval also ends at 6, so the next interval on the second list starts after it and nothing is missed.
Complexity
Interval List Intersections FAQ
What if the lists were not sorted, or intervals inside a list could overlap?
Then the step rule is no longer safe, because a dropped interval could still meet something further on. A sweep fixes it: turn every interval into a +1 event at its start and a −1 event at its end, sort the events, and report the stretches where both lists have a positive count. That costs O((m + n) log(m + n)).
How is this different from Merge Intervals?
Merge Intervals (LeetCode 56) takes one list and returns the union, joining intervals that overlap. Here there are two lists and the answer is only the parts both cover, so each piece of the answer is never wider than either interval it came from.