Show problems as
Classic problem #1413EasyArrayPrefix Sum

Minimum Value to Get Positive Step by Step Sum

Real-world version: The Smallest Opening Balance That Never Dips
Tracked in this browser only
Read Minimum Value to Get Positive Step by Step Sum on LeetCode(opens in a new tab)

The 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.

Python
def min_start_value(changes: list[int]) -> int:
Java
public int minStartValue(int[] changes)