Web Analytics

๐ŸŽฏ Heap Applications

Intermediate to Advanced ~35 min read

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

  1. Maintain min heap of size K
  2. For each element: add to heap
  3. If size > K, remove minimum
  4. 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

  1. Add first element from each list to min heap
  2. Pop minimum โ†’ add to result
  3. Add next element from same list
  4. 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

  1. Sort meetings by start time
  2. Use min heap to track end times
  3. If earliest end โ‰ค current start, reuse room
  4. Otherwise, allocate new room
  5. 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

Output
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:

  1. K Largest: Min heap of size K - O(n log k)
  2. Merge K Lists: Min heap - O(N log k)
  3. Running Median: Two heaps - O(log n) add, O(1) median
  4. Meeting Rooms: Min heap of end times - O(n log n)
  5. Pattern: "Top K" โ†’ heap of size K
  6. Pattern: "Median/middle" โ†’ two heaps
  7. 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!