DSA Master › Topic 9

Queue, Deque & Heap

A heap gives you the min or max element in O(log n) per operation. For top-K, keep a heap of size K. For a running median, use two heaps (a max-heap for the lower half, a min-heap for the upper half). For K-way merges, push the head of each list.

Concepts

How to recognise it

Common pitfalls

Problems

  1. Implement Queue using Stacks (Easy)
  2. Kth Largest Element in an Array (Medium)
  3. Find Median from Data Stream (Hard)
  4. Merge k Sorted Lists (Hard)
  5. Task Scheduler (Medium)
  6. K Closest Points to Origin (Medium)
  7. Reorganize String (Medium)
  8. Find K Pairs with Smallest Sums (Medium)