stones[i] is the weight of stone i. Each turn take the two heaviest stones and smash them together. If their weights are equal both are destroyed; otherwise the lighter one is destroyed and the heavier one keeps the difference of the weights. Return the weight of the last remaining stone, or 0 if none is left.
For [2, 7, 4, 1, 8, 1] the answer is 1.
The starter code sorts all stones after every smash. It is correct but too slow for many stones.
Example 1
Input:
last_stone_weight([2, 7, 4, 1, 8, 1])
Output:
1
Example 2
Input:
last_stone_weight([1])
Output:
1
0 <= len(stones) <= 1000001 <= stones[i] <= 10**9No results yet
Run your code to check it against the examples, or submit to run every test.