Skip to content
BytePatterns

Overlap of Two Span Lists

MediumIntervals#intervals#two-pointers~25m

Problem

Two people share their free time as lists of closed spans [start, end]. Each list is sorted by start, and the spans within one list never overlap. Return the spans when both are free, sorted by start. A span that shrinks to a single point, such as [5, 5], still counts.

Examples

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]]
Input:  a = [[1, 7]], b = [[3, 10]]
Output: [[3, 7]]
Why:    the overlap starts at the later start and ends at the earlier end
Input:  a = [[1, 3], [5, 9]], b = []
Output: []
Why:    edge case, one person is never free

Hints

0 / 3

Stuck on the idea rather than the code? Merge Intervals covers it.