Always grabbing the smallest or largest fast: heapq, two heaps and priorities.
Key ideas
heapqhands you the smallest in O(log n):heappush,heappop,heap[0].- For the largest, push negated values.
- A heap of size k answers "the k largest" over a stream without growing memory.
Pattern
import heapq heap = [] heapq.heappush(heap, (priority, item)) priority, item = heapq.heappop(heap) # the smallest priority smallest = heap[0] # peek without removing
Finish these first:Stacks & Queues
Lessons
Each lesson explains one idea from zero and ends with a few tasks. Lessons open in order; the explanation can be read at any time.
Boss
03
Running Median
Function
The boss opens when every lesson of the topic is finished. Solve it too and the topic is complete, and the topics after it open.