Skip to the code
cf.
+10 XP

#41 Lowest Starting Credit

easy

A game gives the player some starting credit, then applies the changes in changes one after another (positive adds credit, negative spends it). The player loses if their credit ever drops below 1.

Write lowestStartingCredit(changes) that returns the smallest positive whole number of starting credit that keeps the credit at 1 or more after every change.

Example 1

Input: changes = [-2,3,-4,1]
Output: 4

The running total dips to -3 after the third change, so the start must be at least 1 - (-3) = 4.

Example 2

Input: changes = [5,2]
Output: 1

The credit only goes up, so the smallest positive start, 1, works.

Constraints

  • 0 <= changes.length <= 10^5
  • -1000 <= changes[i] <= 1000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).