Final exam — Heap
Three unseen problems (not covered in the lessons) to test whether you leave this section able to solve any problem of the topic: one easy, one medium and one hard. Some test cases are hidden — like the real LeetCode. Solve in Python or JavaScript.
Last Stone Weight
Each turn, take the two heaviest stones x ≤ y and smash them: if x == y, both vanish; otherwise, one of weight y - x remains. Repeat until at most one is left. Return the weight of the last stone (or 0).
Example: [2,7,4,1,8,1] → 1.
Minimum Cost to Connect Sticks
Connecting two sticks of sizes a and b costs a + b and produces a stick of size a + b. Connect them all into one, with minimum total cost, and return that cost.
Example: [2,4,3] → 14 (2+3=5, then 5+4=9; total 5+9=14).
Kth Smallest Element in a Sorted Matrix
Given an n × n matrix in which each row and each column are sorted increasingly, return the k-th SMALLEST element (in global order).
Example: [[1,5,9],[10,11,13],[12,13,15]], k = 8 → 13.