20/37
Part 4 of 6 · Time limitsLearn and do

A min-heap keeps the soonest expiry at heap[0]

A sweep should cost what it removes. That needs the entries in expiry order, soonest first, and a min-heap is the tool: a list that heapq keeps arranged so the smallest item is always heap[0]. heapq.heappush adds an item in O(log n), and heapq.heappop removes the smallest in O(log n), where n is the heap's size.

Predict

The heap holds expiries 3 (b), 10 (a) and 14 (c). purge() runs at time 10. How many heap entries does it pop?

Pick an answer above to see the rest