Heaps & Priority Queues

Heaps give you the min/max in O(1) and add/remove in O(log n). Learn heapify and priority queues.

43 lessons
133 min

Lessons

1. Intro

Efficient extrema access

2m

2. Vocabulary - Heap

Complete binary tree

3m

3. Heap Visualization

See the structure

3m

4. Array Representation

No pointers needed

3m

5. Heap Operations Overview

What heaps support

3m

6. Insert Operation

Bubble up

4m

7. Extract Operation

Bubble down

4m

8. Bubble Down Details

Choosing the right child

4m

9. Vocabulary - Priority Queue

Abstract interface

3m

10. Using Built-in Heaps

Language specifics

3m

11. Quiz: Heap Basics

Time complexity

2m1 problems

12. Problem - Last Stone Weight

Simple heap problem

3m1 problems

13. Last Stone - Algorithm

Max-heap simulation

3m

14. Last Stone - Implementation

Complete solution

3m1 problems

15. Lessons from Last Stone

Repeated max access

2m

16. The K-th Element Problem

Finding rank k

3m

17. K-th Largest with Min-Heap

The counterintuitive choice

4m

18. Problem - Kth Largest Element

Classic heap problem

3m1 problems

19. Kth Largest - Algorithm

Bounded min-heap

3m

20. Kth Largest - Implementation

Complete solution

4m1 problems

21. Lessons from Kth Largest

Heap size matters

3m

22. Quiz: K-th Element

Heap type choice

2m1 problems

23. Problem - Top K Frequent Elements

Frequency + heap

3m1 problems

24. Top K Frequent - Algorithm

Two-phase approach

3m

25. Top K Frequent - Implementation

Complete solution

4m1 problems

26. Lessons from Top K Frequent

Combining structures

2m

27. The Merge Problem

K-way merge

3m

28. K-Way Merge with Heap

The algorithm

4m

29. Problem - Merge K Sorted Lists

Classic interview problem

3m1 problems

30. Merge K Lists - Implementation

Complete solution

5m1 problems

31. Lessons from Merge K Lists

Frontier tracking

2m

32. Quiz: K-Way Merge

Complexity analysis

2m1 problems

33. The Median Problem

Running median challenge

3m

34. Two Heaps Technique

Split at the median

4m

35. Two Heaps Visualization

See the split

3m

36. Problem - Find Median from Data Stream

Two heaps in action

3m1 problems

37. Median Stream - Invariants

What to maintain

4m

38. Median Stream - Implementation

Complete solution

5m1 problems

39. Lessons from Median Stream

Two heaps pattern

3m

40. Quiz: Two Heaps

Insertion order

2m1 problems

41. When to Use Heaps

Pattern signals

2m

42. Heap vs Sorting

When each wins

3m

43. Section Recap

What we learned

3m

Practice Problems

1.
Heap OperationsCodeforcesmedium

Perfect introduction to heap operations (insert, getMin, removeMin) with a greedy approach. Construct valid operation sequences.

2.
MultisetCodeforcesmedium

Excellent k-th order statistics problem. Forces you to use Fenwick Trees or creative frequency array approaches.

3.
PlaylistCodeforcesmedium

Classic greedy with priority queue. Maximize pleasure by sorting by one dimension while maintaining heap for the other.

4.
Maximum MedianCodeforcesmedium

Combines binary search with greedy heap thinking. Maximize median values by focusing operations on elements from median upward.

5.
Two TVsCodeforcesmedium

Perfect interval scheduling simulation using priority queues. Teaches sweep line algorithm with min-heap for active intervals.

6.

Prim/Dijkstra-style graph traversal using priority queues. Find lexicographically smallest paths.

7.

Interval intersection problem requiring greedy segment selection. Use sorting and binary search with multisets.

8.
Color the FenceCodeforcesmedium

Greedy digit construction problem. Maximize number's magnitude while respecting resource constraints.

9.
Linova and KingdomCodeforcesmedium

Tree DP with greedy selection using depth and subtree sizes. Select k nodes that maximize total happiness.

10.
Pasha and TeaCodeforceseasy

Sorting-based greedy optimization. Great introduction to problems where sorting reveals the greedy strategy.

11.
Cunning GenaCodeforceshard

Bitmask DP combined with sorting and greedy heap optimization. Sort by one dimension for efficient DP on subsets.

12.
Heidi and LibraryCodeforceshard

Cache replacement with variable costs - weighted LRU algorithm. Requires minimum cost flow or clever greedy heap strategies.

13.

Fundamental problem teaching min-heap to maintain k largest elements in O(n log k) time. Essential Top-K pattern.

14.

Classic two-heap problem (max-heap + min-heap). Teaches advanced heap coordination and balancing for streaming data.

15.

Fundamental FAANG interview problem demonstrating heap-based merging. Widely applicable in system design scenarios.

16.

Combines hash maps with heaps for frequency-based problems. The powerful 'heap + hashmap' pattern.

17.
Task SchedulerLeetCodemedium

Advanced scheduling combining heaps with greedy algorithms. Simulate CPU task scheduling with cooldown constraints.

18.
Reorganize StringLeetCodemedium

Greedy frequency-based rearrangement using max-heap. Solve character spacing problems to avoid adjacency constraints.

19.

Complex multi-list merging with min-heap and sliding window. Maintain range invariants while processing sorted sequences.

20.
IPOLeetCodehard

Advanced greedy + heap for capital maximization. Combine sorting with heap-based selection for optimal profit.

Ready to start learning?

Access all 43 lessons with interactive content and progress tracking.