Find the Highest Altitude
Real-world version: Peak Balance On A LedgerThe 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.
def peak_balance(deltas: list[int]) -> int:public int peakBalance(int[] deltas)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 9 test cases: 9 passed
- Passed: 4 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's