Description
Given an array of integers nums, you start with an initial positive value startValue.
In each iteration, you calculate the step by step sum of startValue plus elements in nums (from left to right).
Return the minimum positive value of startValue such that the step by step sum is never less than 1.
Example 1:
1 | Input: nums = [-3,2,-3,4,2] |
Example 2:
1 | Input: nums = [1,2] |
Example 3:
1 | Input: nums = [1,-2,-3] |
Constraints:
1 | 1 <= nums.length <= 100 |
Solution
The smallest positive integer is required.
We can get the smallest negative sum, and the answer is the opposite of the negative number + 1
1 | # O(n) time | O(1) space |