week 10 of 15
Heaps and the two medians
Get comfortable with these coding mechanics first: priority queue push, pop and peek. Tuples compare by first element. A max-heap is a min-heap of negated values. Keeping a heap at size k. Template to type from memory by the end of the week: heap of size k: push, and if the size exceeds k, pop. The heap then holds the k best.
- 84Kth Largest Element in an Array (opens LeetCode in a new tab)Min-heap of size kMedium
- Common mistake
- Using a max-heap. A min-heap of size k keeps the k largest.
- 85K Closest Points to Origin (opens LeetCode in a new tab)Heap of size kMedium
- Common mistake
- Getting the tuple order wrong (dist, x, y), or forgetting the negation.
- 86Merge k Sorted Lists (opens LeetCode in a new tab)Min-heap across listsHard
- Common mistake
- Ties compare the node objects and crash. Include an index.
- 87Task Scheduler (opens LeetCode in a new tab)Counting formula or heapMedium
- Common mistake
- Getting the formula wrong. Draw the idle slots.
- 88Meeting Rooms II (opens LeetCode in a new tab)Sort + min-heap of end timesMedium
- Common mistake
- Pushing before popping.
- 89Find Median from Data Stream (opens LeetCode in a new tab)Two heapsHard
- Common mistake
- Rebalancing in the wrong order, or forgetting to negate for the max-heap.
- 90Median of Two Sorted Arrays (opens LeetCode in a new tab)Binary search on a partition (hard)Hard
- Common mistake
- Edge partitions. This is a capstone: skip it and return if you are stuck.