Final Tank Level
A water tank holds at most capacity liters and starts with start liters in it. flows lists changes applied one after another: a positive value pours that many liters in, a negative value drains that many liters out.
Water that would overflow the capacity is lost, and a drain can only take what is in the tank, so the level never goes below 0 or above capacity. Return the level after the last flow; with no flows return start.
Example 1
flows [5,-3,10]
start 2
capacity 8
returns 8
2 + 5 = 7, then 7 - 3 = 4, then 4 + 10 would be 14 but the tank holds only 8.
Example 2
flows [-4,1]
start 3
capacity 10
returns 1
Draining 4 from 3 liters empties the tank to 0, then 1 liter goes in.
Constraints
0 <= len(flows) <= 100-1000 <= flows[i] <= 10001 <= capacity <= 10000 <= start <= capacity
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.