warming up your workspace

Heap

Applies to: general, python

A heap is a tree-shaped array that keeps the smallest (min-heap) or largest (max-heap) element at the root, so the extreme is available in O(1) and push/pop cost O(log n). It is the structure behind priority queues and top-K problems.

import heapq
h = []
heapq.heappush(h, 5)
smallest = heapq.heappop(h)

See also: priority-queue, big-o