Minimum Removals for Conflict-Free Time Slots
Greedy & IntervalsAuto-graded · 19 test casesLast updated
Tests the greedy earliest-finish strategy: find the fewest ranges to discard so the rest share no interior points, with touching endpoints allowed.
Minimum Removals for Conflict-Free Time Slots
You are given a list of booking requests. Each request is a pair [begin, finish] describing a segment on a number line.
Two requests conflict when their segments share more than a single point. Precisely, for requests [a, b] and [c, d] with a <= c, they conflict if and only if c < b. If c == b, the segments only touch at one point and they do not conflict.
Your task is to discard as few requests as possible so that no two of the remaining requests conflict. Return the number of requests you discard.
Identical requests (same [begin, finish]) conflict with each other, so among a group of identical requests at most one can be kept.
Function
Implement min_removals(slots), where slots is a list of [begin, finish] pairs. Return a single integer: the minimum number of requests that must be removed.
Constraints
1 <= len(slots) <= 2 * 10^5- Each element of
slotshas exactly two integers[begin, finish]. -10^9 <= begin < finish <= 10^9- Duplicate requests may appear.
- If the requests are already pairwise conflict-free, the answer is
0.
Examples
Example 1
Input: min_removals([[1, 2], [2, 3], [3, 4], [1, 3]])
Output: 1
Example 2
Input: min_removals([[1, 2], [1, 2], [1, 2]])
Output: 2
Example 3
Input: min_removals([[-50, -20], [-30, 10], [-20, 0], [0, 50]])
Output: 1
Hints
Hint 1
Removing the fewest requests is the same as keeping the largest possible set of requests that do not conflict. The answer is the total count minus the size of that set.
Hint 2
When choosing which request to keep first, which one leaves the most room for later requests? Think about sorting by one of the two endpoints.
Hint 3
Sort by finish time and scan, remembering the finish of the last kept request. Keep a request when its begin is at least that finish value (touching is fine); otherwise count it as removed. Do not initialize the tracked finish to 0, since coordinates can be negative.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Discarding the fewest requests is equivalent to keeping the most requests that are pairwise compatible. If the largest compatible set has size k, the answer is n - k. Finding k is the classic interval scheduling problem, which a greedy strategy solves exactly.
- Sort the requests by finish value, ascending.
- Scan the sorted list while tracking
last_finish, the finish of the most recently kept request (initially nothing is kept). - For each request, if nothing has been kept yet or
begin >= last_finish, keep it and updatelast_finish = finish. Otherwise it conflicts with the kept request, so it is removed. - Return
n - kept.
The comparison uses >= because requests that only touch at an endpoint do not conflict. A sentinel of None is used instead of 0 because coordinates may be negative.
Why it is correct
Exchange argument: let g be the request with the smallest finish. Take any optimal compatible set and let o be its request with the earliest finish. Since g finishes no later than o, and every other request in the optimal set begins at or after finish(o), swapping o for g keeps the set valid and the same size. So some optimal set contains g. After committing to g, only requests beginning at or after finish(g) remain usable, which is the same problem on a smaller input, and induction finishes the proof.
Sorting by begin fails: for [[1, 10], [2, 3], [4, 5]] it keeps [1, 10] first and blocks everything else, while sorting by finish keeps [2, 3] and [4, 5].
Ties in finish do not matter: once one request with finish e is kept, any other request with the same finish begins before e and is rejected.
Complexity
- Time:
O(n log n)for the sort plusO(n)for the scan. - Space:
O(n)for the sorted copy (the input is not modified).
Reference solution
from typing import List
def min_removals(slots: List[List[int]]) -> int:
ordered = sorted(slots, key=lambda s: s[1])
kept = 0
last_finish = None
for begin, finish in ordered:
if last_finish is None or begin >= last_finish:
kept += 1
last_finish = finish
return len(slots) - kept