Heaps and Priority Queues
84 / 170Heaps and Priority QueuesLesson 4 of 8About 45 min
A heap plus a waiting area, which is the shape of nearly every scheduling problem.
heapq only ever gives back the smallest item. The counts here are negated so that the largest one surfaces. Type the three lines it prints.
import heapq remaining = {"a": 3, "b": 1, "c": 2} heap = [-count for count in remaining.values()]heapq.heapify(heap)print(heap[0], "is on top") taken = heapq.heappop(heap)print("popped", taken, "which means a real count of", -taken) heapq.heappush(heap, taken + 1) # ran it once, so one fewerprint(sorted(-value for value in heap))Read the constraints first and let them tell you what complexity is expected. Derive the approach, implement it, run the tests, and submit when it passes.