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.
Keep a min-heap of size k. Push new items and pop when size exceeds k.
You only keep the best k items so far, which keeps memory tight and performance predictable.