Skip to content
BytePatterns

Smallest Start for a Positive Walk

EasyGreedy#running-sum#prefix-minimum~10m

Problem

You pick a positive whole number as a starting value, then add the numbers of a list to it one at a time, from left to right. Return the smallest starting value for which the running total never drops below 1 at any step.

Examples

Input:  nums = [-3, 2, -3, 4, 2]
Output: 5
Why:    5 goes to 2, 4, 1, 5, 7, while 4 would reach 0 after the third step
Input:  nums = [1, 2]
Output: 1
Why:    the total only grows, so the smallest positive start works
Input:  nums = [1, -2, -3]
Output: 5
Why:    edge case, the lowest point comes at the very end

Hints

0 / 3

Stuck on the idea rather than the code? Gas Station covers it.