Last Stone Weight
Repeatedly remove the two heaviest stones. Equal weights both disappear; otherwise insert their positive difference. Return the last weight or zero when none remain.
Example 1
stones = [3,7,2,7]1Explanation
The two 7s cancel, then 3 and 2 leave weight 1.
Example 2
stones = [5,5]0Explanation
Equal weights both disappear, leaving answer zero.
Example 3
stones = [9]9Explanation
A single stone needs no smash.
Constraints
- 1 <= number of stones <= 30
- Weights are 1..1000
Function contract
lastStoneWeightArguments: jsonSolutionRuns on your device
Loading draft…
Run: 3 examples; Submit: 35 cases, on your device.
[3,7,2,7]