You are given a collection of stones in stones, where each integer represents one stone's weight. In one operation, choose any two remaining stones and smash them together. If their weights are equal, both disappear; otherwise, the lighter stone disappears and the heavier stone is replaced by a stone whose weight is the difference between the two.
Continue until at most one stone remains. Choose the operations so that the weight of the final stone is as small as possible. Return that minimum possible weight as an integer. If no stone remains, return 0.
Example 1:
Input: stones = [2,3,7]
Output: 2
Explanation: The stones can be divided into groups with weights 7 and 5, so repeated smashing can leave a stone weighing 2. No arrangement produces a smaller difference.
Example 2:
Input: stones = [1,1,1,1,1]
Output: 1
Explanation: Three stones can be balanced against two stones, giving group weights 3 and 2 and a final difference of 1.
Example 3:
Input: stones = [4,9,3,8]
Output: 0
Explanation: The stones can be split into groups of equal total weight: 4 + 8 and 9 + 3. Smashing within and between the groups can eliminate every stone.
Constraints:
1 <= stones.length <= 301 <= stones[i] <= 1000 contains only integersThe input is provided as a JSON array under the key stones``Need help or want more practice?