13/42
Part 4 of 8 · The sweepLearn and do
Count the most intervals open at once with a sweep
A pool of machines takes reservations in half-open hours. How many machines does it need? That's the most reservations open at the same moment, the question Meeting Rooms II asks.
Counting the overlaps of each interval can give too many
res = [(1, 10), (1, 2), (5, 6)]
for a in res:
print(a, 1 + sum(overlaps(a, b) for b in res if b != a))
(1, 10) 3
(1, 2) 2
(5, 6) 2
The obvious answer counts, for each reservation, how many others overlap it. Then it takes the largest count, plus one for the reservation itself. It's O(n²), and it's wrong.
The long reservation overlaps both short ones, so it scores 3.
Reservations (1, 10), (1, 2) and (5, 6). What's the most open at any one moment?
Pick an answer above to see the rest