Last Stone Weight

Easy · Heap & Priority Queue · about 10 minutes

Asked at Amazon, Google.

Each stone has a positive integer weight. Repeatedly pick up the two heaviest stones, x <= y, and smash them: equal stones both disappear, otherwise one stone of weight y - x remains. When at most one stone is left, return its weight, or 0 if there are none.

Related guides