Last stone weight
A pile of stones lies on a table, and the list stones holds their weights. A round goes like this: take the two heaviest stones off the pile and crash them into each other. If they weigh the same, both turn to dust. If not, the lighter one turns to dust and the heavier one loses as much weight as the lighter one had: stones of 7 and 3 leave one stone of 4, which goes back on the pile.
Keep playing rounds while two or more stones are on the pile. Then return the weight of the stone that is left, or 0 if the pile is empty. Leave the list stones itself unchanged.
stones = [4, 10, 3, 7]Output2Smash 10 and 7: a 3 goes back, so the pile is 4, 3, 3. Smash 4 and 3: a 1 goes back, so the pile is 3, 1. Smash 3 and 1: a 2 is left alone.
stones = [5, 5]Output0Equal stones destroy each other, so nothing is left.
stones = [9]Output9One stone needs no round.
1 ≤ len(stones) ≤ 3 × 104
1 ≤ stones[i] ≤ 105
Plan it first
Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.