Minimum Value to Get Positive Step by Step Sum - Problem
Imagine you're managing a bank account that must never go below $1. You know exactly what deposits and withdrawals are coming, but you need to determine the minimum starting balance to keep your account positive throughout all transactions.
Given an array of integers nums representing your upcoming transactions (positive for deposits, negative for withdrawals), find the minimum positive starting value such that your running balance never drops below 1 after any transaction.
Example: If your transactions are [-3, 2, -3, 4, 2] and you start with $5:
- After -3: balance = 5 + (-3) = 2 ✓
- After +2: balance = 2 + 2 = 4 ✓
- After -3: balance = 4 + (-3) = 1 ✓
- After +4: balance = 1 + 4 = 5 ✓
- After +2: balance = 5 + 2 = 7 ✓
Your task is to find the minimum starting value that keeps all step-by-step sums ≥ 1.
Input & Output
example_1.py — Basic Case
$
Input:
nums = [-3,2,-3,4,2]
›
Output:
5
💡 Note:
Starting with 5: 5→2→4→1→5→7. The minimum value reached is 1, which satisfies our constraint.
example_2.py — All Positive
$
Input:
nums = [1,2]
›
Output:
1
💡 Note:
All elements are positive, so starting with 1 gives us: 1→2→4. All values stay above 1.
example_3.py — Large Negative
$
Input:
nums = [1,-2,-3]
›
Output:
5
💡 Note:
Need to handle the cumulative negative impact: 5→6→4→1. Minimum reached is 1.
Constraints
- 1 ≤ nums.length ≤ 100
- -100 ≤ nums[i] ≤ 100
- The answer is guaranteed to fit in a 32-bit signed integer
Visualization
Tap to expand
Understanding the Visualization
1
Start tracking
Begin with balance = startValue, track minimum balance reached
2
Process transactions
For each transaction, update balance and track minimum
3
Calculate minimum start
Ensure minimum balance reached is at least $1
Key Takeaway
🎯 Key Insight: Track the minimum running sum during simulation. The minimum starting value is simply `max(1, 1 - minimum_running_sum)` to ensure the lowest point never drops below 1.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code