1 min read

Top-K with Heaps

Table of Contents

Approach

Keep a min-heap of size k. Push new items and pop when size exceeds k.

Why it works

You only keep the best k items so far, which keeps memory tight and performance predictable.