LeetCode #986 Medium

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.
Constraints
  • 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
arraytwo-pointersinterval
Open on LeetCode ↗
02

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.

How to spot this pattern

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.

03

Approach

Try it first

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?

1

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.

2

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.

3

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.

4

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.

04

Code in Python, C++ and Java

▶1from typing import List
▶2 
▶3 
▶4class Solution:
▶5 def intervalIntersection(
▶6 self, firstList: List[List[int]], secondList: List[List[int]]
▶7 ) -> List[List[int]]:
▶8 result = []
▶9 i = j = 0
▶10 while i < len(firstList) and j < len(secondList):
▶11 start = max(firstList[i][0], secondList[j][0])
▶12 end = min(firstList[i][1], secondList[j][1])
▶13 if start <= end:
▶14 result.append([start, end])
▶15 if firstList[i][1] < secondList[j][1]:
▶16 i += 1
▶17 else:
▶18 j += 1
▶19 return result
firstsecond0–2i5–1013–2324–251–5j8–1215–2425–26sharedanswerstart at the first interval of each list
i0
j0
Both lists are sorted, so the two indexes only ever move forward. Each comparison looks at one interval from each list.
firstsecond0–2i5–1013–2324–251–5j8–1215–2425–26shared1later startto2earlier end→1–2answer1–20–2 and 1–5 share 1–2
start1
end2
added1–2
The common part starts at the later start, 1, and stops at the earlier end, 2. Start is not past end, so 1–2 is recorded.
firstsecond0–25–10i13–2324–251–5j8–1215–2425–26sharedanswer1–20–2 ends first → drop it
i1
0–2 ends at 2, before 1–5 does. Every later interval in the other list starts after 5, which is past 2, so 0–2 has nothing left to share. 1–5 stays, since it may still reach the next interval.
firstsecond0–25–10i13–2324–251–5j8–1215–2425–26shared5later startto5earlier end→5–5answer1–25–55–10 and 1–5 share 5–5
start5
end5
added5–5
The common part starts at the later start, 5, and stops at the earlier end, 5. Start equals end: the intervals meet at one number, and closed intervals count that point, so 5–5 is recorded.
firstsecond0–25–10i13–2324–251–58–12j15–2425–26sharedanswer1–25–51–5 ends first → drop it
j1
1–5 ends at 5, before 5–10 does. Every later interval in the other list starts after 10, which is past 5, so 1–5 has nothing left to share. 5–10 stays, since it may still reach the next interval.
firstsecond0–25–10i13–2324–251–58–12j15–2425–26shared8later startto10earlier end→8–10answer1–25–58–105–10 and 8–12 share 8–10
start8
end10
added8–10
The common part starts at the later start, 8, and stops at the earlier end, 10. Start is not past end, so 8–10 is recorded.
firstsecond0–25–1013–23i24–251–58–12j15–2425–26sharedanswer1–25–58–105–10 ends first → drop it
i2
5–10 ends at 10, before 8–12 does. Every later interval in the other list starts after 12, which is past 10, so 5–10 has nothing left to share. 8–12 stays, since it may still reach the next interval.
firstsecond0–25–1013–23i24–251–58–12j15–2425–26shared13later startto12earlier end→noneanswer1–25–58–1013–23 and 8–12 share nothing
start13
end12
The common part starts at the later start, 13, and stops at the earlier end, 12. Start is past end, so this pair has no number in common.
firstsecond0–25–1013–23i24–251–58–1215–24j25–26sharedanswer1–25–58–108–12 ends first → drop it
j2
8–12 ends at 12, before 13–23 does. Every later interval in the other list starts after 23, which is past 12, so 8–12 has nothing left to share. 13–23 stays, since it may still reach the next interval.
firstsecond0–25–1013–23i24–251–58–1215–24j25–26shared15later startto23earlier end→15–23answer1–25–58–1015–2313–23 and 15–24 share 15–23
start15
end23
added15–23
The common part starts at the later start, 15, and stops at the earlier end, 23. Start is not past end, so 15–23 is recorded.
firstsecond0–25–1013–2324–25i1–58–1215–24j25–26sharedanswer1–25–58–1015–2313–23 ends first → drop it
i3
13–23 ends at 23, before 15–24 does. Every later interval in the other list starts after 24, which is past 23, so 13–23 has nothing left to share. 15–24 stays, since it may still reach the next interval.
firstsecond0–25–1013–2324–25i1–58–1215–24j25–26shared24later startto24earlier end→24–24answer1–25–58–1015–2324–2424–25 and 15–24 share 24–24
start24
end24
added24–24
The common part starts at the later start, 24, and stops at the earlier end, 24. Start equals end: the intervals meet at one number, and closed intervals count that point, so 24–24 is recorded.
firstsecond0–25–1013–2324–25i1–58–1215–2425–26jsharedanswer1–25–58–1015–2324–2415–24 ends first → drop it
j3
15–24 ends at 24, before 24–25 does. Every later interval in the other list starts after 25, which is past 24, so 15–24 has nothing left to share. 24–25 stays, since it may still reach the next interval.
firstsecond0–25–1013–2324–25i1–58–1215–2425–26jshared25later startto25earlier end→25–25answer1–25–58–1015–2324–2425–2524–25 and 25–26 share 25–25
start25
end25
added25–25
The common part starts at the later start, 25, and stops at the earlier end, 25. Start equals end: the intervals meet at one number, and closed intervals count that point, so 25–25 is recorded.
firstsecond0–25–1013–2324–251–58–1215–2425–26jsharedanswer1–25–58–1015–2324–2425–2524–25 ends first → drop it
i4
24–25 ends at 25, before 25–26 does. Every later interval in the other list starts after 26, which is past 25, so 24–25 has nothing left to share. 25–26 stays, since it may still reach the next interval.
firstsecond0–25–1013–2324–251–58–1215–2425–26jsharedanswer1–25–58–1015–2324–2425–25answer: 6 shared stretches
answer1–2, 5–5, 8–10, 15–23, 24–24, 25–25
One list has run out, so nothing left in the other has a partner. The answer is [[1, 2], [5, 5], [8, 10], [15, 23], [24, 24], [25, 25]], in order because both lists were read left to right.
05

Common pitfalls

Dropping one-point overlaps

✗ Wrong
if start < end:
✓ Right
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

✗ Wrong
result.append([start, end])
i += 1
j += 1
✓ Right
if firstList[i][1] < secondList[j][1]:
    i += 1
else:
    j += 1

One 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].

06

Edge cases

One list empty, e.g. [] and [[1, 3]]

The loop condition fails at once and the answer is [].

Equal ends, e.g. [[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.

07

Complexity

Time
O(m + n)
Space
O(m + n)
Each comparison moves one index forward, so there are at most m + n − 1 of them. The answer can hold up to m + n − 1 pieces; apart from it the work needs O(1) memory.
08

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.