Top K Elements Pattern: Find the Best K Without Sorting Everything
TLDR: To find the top K largest elements, maintain a min-heap of size K. For every new element, push it onto the heap. If the heap exceeds K, evict the minimum. After processing all N elements, the heap holds exactly the K largest. O(N log K) time — ...
abstractalgorithms.dev16 min read