Heap / Priority Queue
⏱ Time: O(N log K)💾 Space: O(K)
A specialized tree-based data structure that satisfies the heap property. In a min-heap, the root is the minimum element, enabling O(1) retrieval and O(log N) insertion/deletion.
🎯 When to Use
- ✓Top K elements
- ✓Merge K sorted streams
- ✓Task scheduling with priorities
📝 Pseudocode
Heap / Priority Queue TemplateO(N log K)
1function solve(nums, k): 2 minHeap = new MinHeap() 3 for num in nums: 4 minHeap.push(num) 5 if minHeap.size() > k: 6 minHeap.pop() 7 return minHeap.peek()
🏢 Real-World Use Cases
Uber
Nearest Driver Dispatch
Maintaining a priority queue of closest available drivers to dispatch the nearest driver efficiently.