Heaps and Priority Queues
83 / 170Heaps and Priority QueuesLesson 3 of 8About 57 min
The heap orders by whatever you put first in the tuple, which is usually not the data itself.
A heap orders whatever you push. Here the pushed value is a pair whose first element is a computed distance rather than anything present in the input. Type the two lines it prints.
import heapq points = [(3, 3), (5, -1), (-2, 4)] heap = []for x, y in points: heapq.heappush(heap, (x * x + y * y, (x, y))) # computed key first print([entry[0] for entry in sorted(heap)])print(heapq.heappop(heap)[1])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.
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.