Repeatedly smash the two heaviest stones until at most one remains, then return its weight.
You are given an array of stone weights. Repeatedly take the two stones with the largest weights and smash them together.
Continue until there is at most one stone left.
Return the weight of the final stone, or 0 if no stones remain.
stones where stones[i] is the weight of the -th stone.0 if all stones are destroyed.1 <= stones.lengthstones[i] are positive integersExample 1
Input
stones = [2,7,4,1,8,1]
Output
1
Explanation
Take 8 and 7 -> 1 remains: [4,2,1,1,1]. Take 4 and 2 -> 2 remains: [2,1,1,1]. Take 2 and 1 -> 1 remains: [1,1,1]. Take 1 and 1 -> both destroyed: [1]. The last stone weighs 1.
Example 2
Input
stones = [1]
Output
1
Explanation
Only one stone exists, so it is already the final stone.
Example 3
Input
stones = [3,3]
Output
0
Explanation
The two heaviest stones have equal weight, so both are destroyed.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.