Heap / Priority Queue
A heap keeps the smallest (or largest) element instantly accessible while inserts and removals stay O(log n). Use it for top-k, kth-largest, merging sorted streams, or any time you repeatedly need the current minimum or maximum.
16 LeetCode problems solved with the Heap / Priority Queue pattern. Practice them with spaced repetition so the pattern sticks.
- Find K Pairs with Smallest SumsMEDIUM · O(k log k)
- Find Median from Data StreamHARD · O(log n)
- IPOHARD · O((n + k) log n)
- K Closest Points to OriginMEDIUM · O(n log k)
- Kth Largest Element in a StreamEASY · O(log k)
- Kth Largest Element in an ArrayMEDIUM · O(n log k)
- Last Stone WeightEASY · O(n log n)
- Maximum Subsequence ScoreMEDIUM · O(n log n)
- Meeting Rooms IIMEDIUM · O(n log n)
- Meeting Rooms IIIHARD · O(m log m + m log n)
- Merge k Sorted ListsHARD · O(N log k)
- Minimum Interval to Include Each QueryHARD · O((n + q) log(n + q)) where n = intervals, q = queries
- Single-Threaded CPUMEDIUM · O(n log n)
- Smallest Number in Infinite SetMEDIUM · O(log n)
- Top K Frequent ElementsMEDIUM · O(n log k)
- Total Cost to Hire K WorkersMEDIUM · O((k + candidates) log candidates)
See the full solution
The complete approach, reference solutions in 5 languages, and a step-by-step visualization — then add this problem to your spaced-repetition schedule.
Start free →