Loading...
You are given an array of positive integers weights.
Repeat the following step while at least two values remain: remove the two largest values, x and y with x <= y. If x == y, both are gone. If x < y, put the value y - x back into the array.
Eventually at most one value remains. Return that value, or 0 if the array ends up empty.
Input: weights = [2, 7, 4, 1, 8, 1]
Output: 1
Explanation: Combine 8 and 7 leaving 1: [2, 4, 1, 1, 1]. Combine 4 and 2
leaving 2: [2, 1, 1, 1]. Combine 2 and 1 leaving 1: [1, 1, 1]. Combine 1 and
1 leaving nothing: [1]. The last remaining value is 1.
Input: weights = [1]
Output: 1
weights.length ≤30weights[i] ≤1000Click "Run" to test with sample cases or "Submit" to run all tests.