When to Use

Pattern triggers

Look for these signals in the problem:

  • "Kth largest/smallest" element: Min-heap of size k maintains k largest; max-heap maintains k smallest.
  • "Merge k sorted" lists or streams: Heap efficiently tracks minimum across k pointers.
  • "Continuous median" or statistics: Two heaps (max-heap for lower half, min-heap for upper half) maintain balance.
  • "Schedule" or "prioritize" tasks: Heap processes highest priority items first.
  • "Top k frequent" elements: Count first, then heap selects top k by frequency.