All problems
1005EasyArrayPrefix Sum

The Smallest Opening Balance That Never Dips

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1413Minimum Value to Get Positive Step by Step Sum

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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