← practice

Heaps & Priority Queue

top-K · merge-K · streaming
Reach for a heap whenever you see "top K", "K closest", "K most frequent", "merge K", or "running median". A heap gives you the min or max in O(1) and reorders in O(log n). In Java, PriorityQueue is a min-heap; flip the comparator for max-heap. The size-K-heap trick beats full sorting for top-K queries.

6 problems

Next patternBacktracking