41. Last Stone Weight

Each turn, smash the two heaviest stones together. If they weigh the same both are destroyed; otherwise the lighter one is destroyed and the heavier loses that much weight. Return the weight of the last stone, or 0 if none are left.

Example 1

Input: stones = [2,7,4,1,8,1]
Output: 1

Example 2

Input: stones = [1]
Output: 1
Hint 1

You repeatedly need the two largest values: a max-heap gives them in O(log n).

Heap