Top-k problem
Finding the k largest elements of a list of n. With a min-heap of size k it costs O(n log k).
Example
The 10 most-read news stories out of 1 million: with a heap of 10, about 3.3 million steps instead of sorting everything.
Test yourself: Top-k problem
The k largest
Which of these is a top-k problem?
It's picking the k = 10 largest out of n = 5 million. Finding 1 or counting don't ask to keep the best.
The k largest
Picking the maximum k times costs O(nk); if k gets close to n, it becomes O(___).
With n = 10,000 and k = 9,000, that's 90 million steps: almost n² = 100 million.
The k largest
For the top-k largest you use a ___-heap.
The min-heap's root is the worst of the k best: exactly who each newcomer has to be compared with. With k = 10, the 10th.
0/3
â–¶ Play