Skip to content
BytePatterns

Fewest Removals to Unclash

MediumGreedy#greedy#interval-scheduling#sorting~20m

Problem

You are given a list of spans, each written as a start and an end, where the end is always larger than the start. Two spans clash when one begins strictly before the other finishes; touching at a single point is fine. Remove as few spans as possible so that nothing left clashes, and return how many you removed.

Examples

Input:  spans = [(1, 2), (2, 3), (3, 4), (1, 3)]
Output: 1
Why:    dropping (1, 3) leaves three spans that only touch at their ends
Input:  spans = [(1, 2), (1, 2), (1, 2)]
Output: 2
Why:    three identical spans all clash, so only one can survive
Input:  spans = [(1, 2), (2, 3)]
Output: 0
Why:    edge case, touching at a point is not a clash so nothing is removed

Hints

0 / 3

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