The Top K Pattern in Depth
"Find the k largest", "the k most frequent" and "the k closest" are all the same problem in disguise. Here is how to solve all of them with one template.
What these problems look like
You are given n items and asked for the k best ones by some measure. The quick answer is to sort everything and slice off the first k, which costs O(n log n).
When k is much smaller than n, sorting does far more work than needed. You only have to remember the k best items seen so far.
A heap of size k does exactly that.
The optimized approach:
- Use a min heap for the k largest items, and a max heap for the k smallest.
- Push each item onto the heap.
- When the heap holds more than k items, pop its top. That removes the worst of the current candidates.
- After the scan, the heap holds the answer. In Python:
heapq.heappush(heap, x), thenheapq.heappop(heap)wheneverlen(heap) > k.
Complexity Analysis:
- Every push and pop is O(log k)
- The heap never holds more than k + 1 items
- Time: O(n log k) for n pushes and at most n pops
- Space: O(k) for the heap itself