Practice Discrete Math

Algorithms / Heap Frontier Top K

Least You Need to Know: K-Way Merge, Top-K, and Heap Frontier Ideas

Heaps are useful when many sorted or partially ordered sources expose only a few promising next candidates. The heap stores the current **frontier** so you can pop the next best item without materializing every unseen option.

The least you need to know

Key notation

frontier current best visible candidates from all sources
k-way merge merge multiple sorted streams into one order
top-k only the k best items are required

Tiny worked example

  • Suppose you have k sorted log streams and need the global next event each time.
  • Put the first item from each stream into a min-heap.
  • Pop the smallest visible item.
  • Then push only the next item from that same stream.
  • The heap never needs to hold every unseen item at once.

Common mistakes

How to recognize this kind of problem

Start practice