knaxuslearn · build · ship

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.

  1. 84Kth Largest Element in an Array (opens LeetCode in a new tab)Min-heap of size kMediumcore
    Common mistake
    Using a max-heap. A min-heap of size k keeps the k largest.
  2. 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.
  3. 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.
  4. 87Task Scheduler (opens LeetCode in a new tab)Counting formula or heapMedium
    Common mistake
    Getting the formula wrong. Draw the idle slots.
  5. 88Meeting Rooms II (opens LeetCode in a new tab)Sort + min-heap of end timesMediumPremium
    Common mistake
    Pushing before popping.
  6. 89Find Median from Data Stream (opens LeetCode in a new tab)Two heapsHardcore
    Common mistake
    Rebalancing in the wrong order, or forgetting to negate for the max-heap.
  7. 90Median of Two Sorted Arrays (opens LeetCode in a new tab)Binary search on a partition (hard)Hardcore
    Common mistake
    Edge partitions. This is a capstone: skip it and return if you are stuck.