Skip to content
BytePatterns

Cut a Span Out of Sorted Spans

EasyIntervals#interval-overlap#single-pass~15m

Problem

A set of numbers is stored as a sorted list of disjoint half-open spans [a, b), each covering every number from a up to but not including b. Given one more span cut = [lo, hi), remove every number it covers from the set and return what is left, again as a sorted list of disjoint spans.

Examples

Input:  spans = [[0, 2], [3, 4], [5, 7]], cut = [1, 6]
Output: [[0, 1], [6, 7]]
Why:    the cut trims the first span, swallows the second and trims the third
Input:  spans = [[0, 5]], cut = [2, 3]
Output: [[0, 2], [3, 5]]
Why:    a cut inside one span splits it into two
Input:  spans = [[-5, -4], [-3, -2], [1, 2], [3, 5], [8, 9]], cut = [-1, 4]
Output: [[-5, -4], [-3, -2], [4, 5], [8, 9]]
Why:    edge case, spans entirely outside the cut pass through untouched

Hints

0 / 3

Stuck on the idea rather than the code? Insert Interval covers it.