Minimum Value to Get Positive Step by Step Sum (LeetCode 1413): Solution and Explanation
Find the smallest positive starting value that keeps a running total at 1 or more after every step. The answer is 1 minus the lowest prefix sum, or 1 if no prefix sum goes negative.
Intuition
Picture the running total as a path that goes up and down as you add each number. Changing the starting value does not change the shape of that path. It only slides the whole path up or down.
So walk the path once starting from 0 and remember its lowest point. To keep every step at 1 or more, the start has to lift that lowest point to exactly 1.
If the path never dips below 0, no lift is needed beyond the rule that startValue itself must be positive, which gives 1.
Approach: Lowest prefix sum
- Set total = 0 and lowest = 0. Starting lowest at 0 covers the case where no prefix is negative.
- For each x in nums, add x to total and set lowest = min(lowest, total).
- Return 1 - lowest.
Step-by-step example
nums = [-3,2,-3,4,2]
- Add -3: total = -3, lowest = -3.
- Add 2: total = -1, lowest stays -3.
- Add -3: total = -4, lowest = -4.
- Add 4: total = 0. Add 2: total = 2. lowest stays -4.
- Return 1 - (-4) = 5. Starting at 5 the totals are 2, 4, 1, 5, 7.
Code: Python, Java, C++
1class Solution:
2 def minStartValue(self, nums: List[int]) -> int:
3 total = 0
4 lowest = 0
5 for x in nums:
6 total += x
7 lowest = min(lowest, total)
8 return 1 - lowest1class Solution {
2 public int minStartValue(int[] nums) {
3 int total = 0;
4 int lowest = 0;
5 for (int x : nums) {
6 total += x;
7 lowest = Math.min(lowest, total);
8 }
9 return 1 - lowest;
10 }
11}1class Solution {
2public:
3 int minStartValue(vector<int>& nums) {
4 int total = 0;
5 int lowest = 0;
6 for (int x : nums) {
7 total += x;
8 lowest = min(lowest, total);
9 }
10 return 1 - lowest;
11 }
12};Time and space complexity
O(n)
One pass over nums.
O(1)
Only the running total and the lowest value are stored.
Other approaches
Try start values one by one (time O(n * answer), space O(1))
Start at 1 and simulate; if any total drops below 1, increase the start and try again. Correct but slow, since the answer can reach 10001.
Binary search on the start value (time O(n log(n * 100)), space O(1))
Whether a start works is monotonic: if s works, s + 1 works too. Binary search the smallest working s with an O(n) check.
Common mistakes
- Starting lowest at nums[0] or at positive infinity. Then an all-positive input returns a value below 1 instead of 1.
- Returning -lowest instead of 1 - lowest. The totals must stay at least 1, not at least 0.
- Checking the total before adding the element. The condition applies after each addition.
Frequently asked questions
Why is the answer never less than 1?
lowest starts at 0 and only goes down, so 1 - lowest is always at least 1, which matches the rule that startValue is positive.
Can the answer overflow a 32-bit int?
No. With at most 100 elements of size at most 100, the lowest prefix is at least -10000, so the answer is at most 10001.
Do I need to store the prefix sums?
No. Only the running total and the minimum seen so far matter, so the pass uses constant space.