Skip to content
BytePatterns

Non Overlapping Removals

MediumIntervals#intervals#greedy~25m

Problem

Given a list of intervals [start, finish], return the smallest number of them you must remove so that none of the survivors overlap. Intervals that only touch at an endpoint — one finishing exactly where the next starts — do not count as overlapping.

Examples

Input:  intervals = [[1, 2], [2, 3], [3, 4], [1, 3]]
Output: 1
Why:    dropping [1, 3] leaves three intervals that only touch at endpoints
Input:  intervals = [[1, 2], [1, 2], [1, 2]]
Output: 2
Why:    all three cover the same span, so only one can stay
Input:  intervals = []
Output: 0
Why:    edge case, nothing to remove

Hints

0 / 3

Stuck on the idea rather than the code? Interval Basics & Sorting covers it.