๐ฏ Heap Applications
You're running an e-commerce site. You need to show "Top 10 Products," merge customer activity from multiple servers, and calculate real-time statistics. These aren't just coding problems - they're real business needs that heaps solve brilliantly!
Why Heaps Excel at These Problems
The Heap Advantage
Heaps are perfect when you need:
- Top K items: O(n log k) instead of O(n log n)
- Streaming data: Process items one at a time
- Priority-based: Always access min/max in O(1)
- Space efficiency: O(k) space for top K problems
See the Applications
Application 1: K Largest Elements
The Problem
Find the K largest elements from a dataset.
Real-world: Top K products, highest scores, trending items, leaderboards
The Solution: Min Heap of Size K
Key insight: Keep only K largest by maintaining a min heap
- Maintain min heap of size K
- For each element: add to heap
- If size > K, remove minimum
- Heap contains K largest!
Why min heap? Smallest of K largest is at top - easy to remove!
Time: O(n log k) vs O(n log n) for sorting
Application 2: Merge K Sorted Lists
The Problem
Merge K sorted lists into one sorted list.
Real-world: Merge log files, combine sorted data streams, database operations
The Solution: Min Heap
- Add first element from each list to min heap
- Pop minimum โ add to result
- Add next element from same list
- Repeat until all elements processed
Time: O(N log k) where N = total elements
Better than: Merging lists pairwise O(N k)
Application 3: Running Median
The Problem
Find median from a continuous stream of numbers.
Real-world: Real-time analytics, monitoring systems, streaming statistics
The Solution: Two Heaps!
Brilliant insight: Use two heaps to split data
- Max heap: Smaller half (largest at top)
- Min heap: Larger half (smallest at top)
Invariant: Heap sizes differ by at most 1
Median: Top of larger heap (or average of both tops)
Time: O(log n) to add, O(1) to get median
Application 4: Meeting Rooms
The Problem
Find minimum meeting rooms needed for given intervals.
Real-world: Conference room scheduling, resource allocation, CPU scheduling
The Solution: Min Heap of End Times
- Sort meetings by start time
- Use min heap to track end times
- If earliest end โค current start, reuse room
- Otherwise, allocate new room
- Heap size = rooms needed
Time: O(n log n)
More Applications
5. Top K Frequent Elements
Use case: Trending topics, popular products
Solution: Count frequencies, use min heap of size K
6. Task Scheduler
Use case: CPU scheduling with cooling periods
Solution: Max heap of task frequencies
7. Kth Largest in Stream
Use case: Leaderboards, ranking systems
Solution: Min heap of size K, always O(1) access
8. K Closest Points
Use case: Location-based services, nearest neighbors
Solution: Max heap of distances, size K
The Complete Code
Click Run to execute your code
Problem-Solving Strategy
When to Use Heaps
- "Top K" or "K largest/smallest": Use heap of size K
- "Merge K sorted": Use min heap
- "Median" or "middle element": Use two heaps
- "Scheduling" or "intervals": Use heap of end times
- "Stream processing": Heaps handle one-at-a-time
Summary
What You've Learned
Heaps solve many practical problems efficiently:
- K Largest: Min heap of size K - O(n log k)
- Merge K Lists: Min heap - O(N log k)
- Running Median: Two heaps - O(log n) add, O(1) median
- Meeting Rooms: Min heap of end times - O(n log n)
- Pattern: "Top K" โ heap of size K
- Pattern: "Median/middle" โ two heaps
- Pattern: "Merge/combine" โ min heap
What's Next?
You've mastered heaps and their applications! Next, we'll explore Tries - a tree structure perfect for string problems like autocomplete and spell checking!
Enjoying these tutorials?