Problem 359329 · easy · Phase 03 Linear Management & Searching

Smallest Safe Opening Balance

prefix sums · prefix min

An account processes transactions in order (positive = deposit, negative = withdrawal). The bank requires the balance to be at least 1 after every transaction. Return the smallest positive integer opening balance that satisfies this rule.

Examples

Input:  transactions = [-3, 2, -3, 4, 2]
Output: 5
Explanation: starting at 5 the balances are 2, 4, 1, 5, 7; starting at 4 would hit 0.

Input:  transactions = [1, 2]
Output: 1

Input:  transactions = [1, -2, -3]
Output: 5

Constraints

  • 0 <= len(transactions) <= 10**5 (an empty list needs an opening balance of 1)
  • -10**4 <= transactions[i] <= 10**4
  • Target complexity: O(n) time, O(1) extra space.

Goals

  • Track the lowest running total
  • Turn a 'never below one' requirement into a formula
Starting Python…