class Solution:
def minStartValue(self, nums):
min_sum = 0
curr_sum = 0
for num in nums:
curr_sum += num
min_sum = min(min_sum, curr_sum)
return 1 - min_sum
class Solution {
public:
int minStartValue(vector<int>& nums) {
int min_sum = 0;
int curr_sum = 0;
for (int num : nums) {
curr_sum += num;
min_sum = min(min_sum, curr_sum);
}
return 1 - min_sum;
}
};
class Solution {
public int minStartValue(int[] nums) {
int minSum = 0;
int currSum = 0;
for (int num : nums) {
currSum += num;
minSum = Math.min(minSum, currSum);
}
return 1 - minSum;
}
}
var minStartValue = function(nums) {
let minSum = 0;
let currSum = 0;
for (let num of nums) {
currSum += num;
minSum = Math.min(minSum, currSum);
}
return 1 - minSum;
};
You are given an integer array nums
. Your task is to find the minimum positive integer startValue
such that when you initialize a variable sum = startValue
and add each element of nums
to sum
in order, the value of sum
is always greater than or equal to 1 at every step.
In other words, for each index i
(from 0 to nums.length - 1
), the running sum startValue + nums[0] + ... + nums[i]
should never be less than 1.
nums
may be negative, zero, or positive.nums
in order.
The first instinct might be to try every possible startValue
from 1 upwards, checking if the running sum ever drops below 1. However, this brute-force approach is inefficient, especially for large arrays or large negative numbers.
Instead, let's think about the problem differently: the only way the running sum can become invalid is if, at some step, the cumulative sum (starting from startValue
) goes below 1. So, if we can find the lowest point the running sum ever reaches (when starting from 0), we can set startValue
just high enough to keep the sum always at least 1.
In other words, we want the minimum startValue
so that startValue + min_prefix_sum >= 1
, where min_prefix_sum
is the minimum value of the cumulative sum of nums
as we iterate through the array.
curr_sum
(current cumulative sum) set to 0, and min_sum
(minimum prefix sum) set to 0.
nums
:
curr_sum
.min_sum
to be the minimum of itself and curr_sum
.min_sum
.
startValue + min_sum >= 1
. So, startValue = 1 - min_sum
.
1 - min_sum
as the answer.
This approach only requires a single pass through the array, making it efficient and simple to implement. No extra data structures are needed.
Let's use the example nums = [-3, 2, -3, 4, 2]
.
curr_sum = 0
, min_sum = 0
.curr_sum = -3
, min_sum = -3
curr_sum = -1
, min_sum = -3
curr_sum = -4
, min_sum = -4
curr_sum = 0
, min_sum = -4
curr_sum = 2
, min_sum = -4
The lowest the running sum ever gets is -4. To ensure the running sum is always at least 1, we need startValue
such that startValue + (-4) >= 1
, so startValue >= 5
.
Therefore, the answer is 5.
startValue
(potentially up to sum of absolute values in nums
), you check the array, leading to O(N * K) time, where K is the value needed to reach a valid startValue
.
nums
.
The key insight is to track the minimum running sum as you iterate through the array. By ensuring the startValue
offsets the lowest dip in the prefix sum, you can guarantee that the running sum never drops below 1. This simple, efficient approach avoids brute-force checks and works in a single pass with constant space, making the solution both elegant and practical.