Predict the output
What does this print?
bookings = [[0, 30], [5, 10], [10, 20], [15, 25]]
starts = [(s, 1) for s, _ in bookings]
ends = [(e, -1) for _, e in bookings]
events = sorted(starts + ends)
live = peak = 0
for _, d in events:
live += d
peak = max(peak, live)
print(peak)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 3
Right: at time 15, three bookings run together.
Ends sorting first at time 10 is what stops [5, 10) and [10, 20) from counting together.
Why not the others?
- B 4. [5, 10) has ended before [10, 20) starts, so all four never overlap.
- C 2. From 15 to 20, [0, 30), [10, 20) and [15, 25) overlap.