Glossary: Coding Interviews in Go
Heap for top k
Also written: min-heap, heap, top k, pattern 5
Keeping a min-heap of at most k items and dropping the smallest whenever it grows past k. It costs O(n log k), which beats sorting when k is small.
Glossary: Coding Interviews in Go
Also written: min-heap, heap, top k, pattern 5
Keeping a min-heap of at most k items and dropping the smallest whenever it grows past k. It costs O(n log k), which beats sorting when k is small.