Show problems as
Classic problem #1732EasyArrayPrefix Sum

Find the Highest Altitude

Real-world version: Peak Balance On A Ledger
Tracked in this browser only
Read Find the Highest Altitude on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

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

Python
def peak_balance(deltas: list[int]) -> int:
Java
public int peakBalance(int[] deltas)