Non-overlapping Intervals
Remove the fewest intervals so that none overlap
DSA, Medium, Intervals
Which structure or algorithm, and what does it cost.
A hall gets booking requests in a list bookings, each as [start, end]. A booking holds the hall from start up to end, but not the moment end itself, so [1,4] and [4,6] do not clash. You may cancel requests. Return the smallest number of requests to cancel so that no two remaining requests overlap. The list is not sorted.
Examples
Constraints
- 0 <= length of
bookings<= 60,000. - 0 <= start < end <= 1,000,000,000.
Your answer must contain
- The simple method and why it is too slow; the better method; code; time and memory cost; a hand trace of example 1.
Target Aim for O(n log n) time.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Cancelling the fewest requests is the same as keeping the most requests. Think about what makes one request a safe first choice to keep.
Hint 2 the approach
Put the requests in order by one of their two times and go through them once. Test your choice of time on a long request that covers several short ones.
Hint 3 the key step
Sort by end time. Keep a request if its start is at or after the last kept end. Otherwise cancel it. The answer is the total minus the kept count.
Walkthrough
Full walkthrough brute force to best
Brute force
Try every subset of the requests as the set to keep, check that no two overlap, and keep the largest valid subset. The answer is the total minus its size. There are 2^n subsets, so 60,000 requests are out of reach.
Better method
Sort by start. Let best[i] be the most requests you can keep when request i is the last one kept. For each i, take the best of the earlier requests that end at or before start i, and add 1. The answer is the total minus the largest best[i]. This is O(n^2) time, about 1.8 billion steps for 60,000 requests, which is still too slow.
Best method
Always keep the request that ends earliest, then repeat on the requests that start at or after its end. In code: sort by end, and keep a request when its start is at or after the last kept end. Why it is right: take any best set of kept requests. Its first request ends no earlier than the request with the earliest end overall. Swap them: the swapped request leaves at least as much room, so the set stays valid and keeps its size. So some best set holds the earliest-ending request, and the same argument holds for what is left. The answer is the total minus the kept count. Sorting by start and always keeping the earlier request fails, because one long early request can block many short ones. Sorting by start works only if, on a clash, you cancel the one that ends later.
Hand trace
Sort example 1 by end: [1,4], [4,5], [3,6], [5,9], [8,12]. Take [1,4]: nothing is kept yet, so keep it. The last end is 4. Take [4,5]: 4 is at least 4, so keep it. The last end is 5. Take [3,6]: 3 is below 5, so cancel it. Take [5,9]: 5 is at least 5, so keep it. The last end is 9. Take [8,12]: 8 is below 9, so cancel it. Three are kept out of five, so the answer is 2.
Code
def min_cancellations(bookings):
kept = 0
last_end = None
for s, e in sorted(bookings, key=lambda b: b[1]):
if last_end is None or s >= last_end:
kept += 1
last_end = e
return len(bookings) - keptTime and memory
O(n log n) time, from the sort. The pass is O(n). O(n) memory for the sorted copy.
Common mistakes
- Sorting by start. With [1,10], [2,3], [4,5], [6,7] the long request is kept first, so three are cancelled instead of one.
- Keeping a request only when its start is above the last end. A request that starts exactly when the last one ends fits, so [1,4] and [4,5] both stay. Use at or after.
- Returning the kept count. The task asks for the number cancelled, which is the total minus the kept count.