Sum Leftmost Subtraction Levels
Problem statement
Repeat until every nonnegative value is zero:
- Choose the leftmost nonzero value x and add x to the result.
- Starting there, subtract x from consecutive values while each current value is at least x; stop before the first smaller value.
Return the accumulated result.
Function
sumSubtractionLevels(values: int[]) → longExamples
Example 1
values = [3,3,5,2,3]return = 6The reported process accumulates six.
Example 2
values = [3,3,5,2,4]return = 7The successive added levels are 3, 2, and 2.
Constraints
1 <= values.length <= 10000 <= values[i] <= 1000