All problems
0334EasyArrayPrefix Sum

Peak Balance On A Ledger

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1732Find the Highest Altitude

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.

A petty-cash ledger is opened with a balance of 0 and then n entries are posted in order. Entry i changes the balance by gain[i], which is negative for money paid out and positive for money taken in.

The balance is only ever inspected between postings, so the figures that matter are the opening 0 and the balance right after each of the n entries.

Return the largest balance the ledger ever shows. Because the opening figure counts, the answer is never below 0.

Examples

Example 1

Input
gain = [3, -2, 5]
Output
6

The balances on the ledger read 0, 3, 1 and 6, and the largest of those four figures is 6.

Example 2

Input
gain = [5, -10, 2]
Output
5

The balances read 0, 5, -5 and -3, so the ledger tops out at 5.

Example 3

Input
gain = [-4, -2, -3]
Output
0

Every posting pays money out, so the balances read 0, -4, -6 and -9 and the opening figure is the largest.

Example 4

Input
gain = [100, -100, 100, -100]
Output
100

The balances read 0, 100, 0, 100 and 0; the ledger reaches 100 twice and never goes higher.

Constraints

  • n == gain.length
  • 1 <= n <= 100
  • -100 <= gain[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 peak_balance(deltas: list[int]) -> int:
Java
public int peakBalance(int[] deltas)
September 7
Apply