Skip to content

10. Heaps

🧠 Strategy

  1. Heaps (Priority Queue):
    • Kth Largest/Smallest: Use a Heap of size K. (Min-Heap for largest, Max-Heap for smallest).
    • Stream Data: Use two heaps (Min & Max) for Running Median.
    • Merge K Sorted: Push the first element of all K arrays into a Min-Heap.
  2. Greedy:
    • Sorting is Key: Most greedy problems start by sorting the data (Start Times, Costs, etc.).
    • Local Optimal: Make the best choice right now (e.g., pick the job ending earliest).

🟢 Heap Basics

🟡 Kth Element & Streams

🟠 Greedy Algorithms