Last Stone Weight
Problem
You have a collection of stones, each with a positive weight. Repeatedly take the two heaviest stones and smash them together: if their weights are equal, both are destroyed; if not, the lighter one is destroyed and the heavier one's weight is reduced by the lighter one's weight. Continue until at most one stone remains, and return its weight, or 0 if none remain.
Example. With stones [2, 7, 4, 1, 8, 1], smashing 8 and 7 leaves a 1, then smashing that new 1 with the existing 4 leaves a 3, then smashing 3 with 2 leaves a 1, and the final smash of 1 and 1 destroys both, leaving weight 0.
Key idea
The process is naturally simulated step by step, but finding the two heaviest stones by scanning the whole collection each round costs O(n) per round. Since the two largest values are needed repeatedly from a collection that keeps changing, this is exactly what a max-heap is built for: it keeps the largest element accessible in constant time and accepts updates cheaply.
Load every stone weight into a max-heap. On each round, pop the two largest weights. If they differ, push the difference back onto the heap as a new stone; if they are equal, push nothing. Repeat until the heap holds zero or one stone, then return the remaining weight or 0. Each round shrinks the heap by one or two stones, so the simulation terminates in at most n rounds.
Solution
Complexity
- Time: O(n log n). Each of up to n rounds does O(1) heap pops and at most one O(log n) push.
- Space: O(n). The heap holds up to n stone weights.
Watch out for
- Many languages only provide a min-heap directly; negate weights on insertion, and negate again on removal, to simulate a max-heap.
- Handle the end state cleanly: return 0 for an empty heap and the stone's weight otherwise.
Pattern
This is a simulation driven by repeatedly needing the current maximum, the signature use case for a heap: whenever "always operate on the current largest or smallest" repeats over changing data, a heap turns each step into a fast, incremental update instead of a full rescan.