Minimum Value to Get Positive Step by Step Sum
Real-world version: The Smallest Opening Balance That Never DipsThe statement here is our own: the same inputs, outputs and technique.
An account is opened holding some whole amount, and then the entries of changes are applied to the balance one at a time, in the order given.
Return the smallest opening amount, itself at least 1, that leaves the balance at 1 or more after every entry has been applied.
Examples
Example 1
- Input
- changes = [-4, 3, -4, 5, 3]
- Output
- 6
The running totals of the entries are -4, -1, -5, 0 and 3, so the deepest the balance ever sits below its opening amount is five. Opening with six leaves the balance at one at that lowest point, and opening with five would leave it at nothing.
Example 2
- Input
- changes = [2, 5]
- Output
- 1
Both entries add, so the balance only climbs and the smallest allowed opening amount already works.
Example 3
- Input
- changes = [2, -3, -4]
- Output
- 6
The running totals are 2, -1 and -5, and the last of those is the deepest, so the opening amount has to cover five and leave one over.
Constraints
- 1 <= changes.length <= 100
- -100 <= changes[i] <= 100
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def min_start_value(changes: list[int]) -> int:public int minStartValue(int[] changes)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's