Eleven problems that drill heaps / top-K from Lesson 14. The one trick to burn in: to keep the k largest elements, use a min-heap of size k and pop whenever it overflows. Read the approach, try it, then check the code — every solution here was run and tested.
How to practise. Before coding, answer two questions: what goes in the heap (a value? a tuple keyed by frequency/distance?) and which direction (min-heap of size k for “k largest”, max-heap via negatives for “repeatedly take the biggest”). Then write it. Stuck for 20+ minutes? Read it, close the page, re-solve from memory the next day.
The 11 problems
- 1Last Stone Weight (LeetCode 1046)EasySmash the two heaviest stones until at most one is left: a max-heap hands you the two biggest each round.arrow_forward
- 2Kth Largest Element in a Stream (LeetCode 703)EasyReport the kth largest value as numbers arrive: keep a min-heap of the k largest; its root is the answer.arrow_forward
- 3Kth Largest Element in an Array (LeetCode 215)MediumFind the kth largest number without sorting: a min-heap capped at size k leaves the answer at its root.arrow_forward
- 4Top K Frequent Elements (LeetCode 347)MediumReturn the k most common values: count with a hash map, then pick the k largest counts.arrow_forward
- 5K Closest Points to Origin (LeetCode 973)MediumReturn the k points nearest the origin: keep a size-k max-heap on squared distance and evict the farthest.arrow_forward
- 6Task Scheduler (LeetCode 621)MediumFewest CPU slots to run tasks with a cooldown: the most frequent task builds the frame, the rest fill gaps.arrow_forward
- 7Reorganize String (LeetCode 767)MediumRearrange a string so no two neighbours match: place the most frequent letter, holding the last one back.arrow_forward
- 8Sort Characters By Frequency (LeetCode 451)MediumReorder a string so the most frequent characters come first: count them, then emit by descending count.arrow_forward
- 9Find K Pairs with Smallest Sums (LeetCode 373)MediumFind the k smallest-sum pairs from two sorted arrays: treat each row as a sorted list and k-way merge them.arrow_forward
- 10Merge k Sorted Lists (LeetCode 23)HardMerge k sorted linked lists into one: keep each list's current head in a min-heap and pop the smallest.arrow_forward
- 11Find Median from Data Stream (LeetCode 295)HardKeep a running median as numbers arrive: a max-heap for the lower half, a min-heap for the upper half.arrow_forward