Skip to content

1413. Minimum Value to Get Positive Step by Step Sum 👍

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
 public:
  int minStartValue(vector<int>& nums) {
    int sum = 0;
    int minSum = 0;

    for (const int num : nums)
      minSum = min(minSum, sum += num);

    return 1 - minSum;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
  public int minStartValue(int[] nums) {
    int sum = 0;
    int minSum = 0;

    for (final int num : nums)
      minSum = Math.min(minSum, sum += num);

    return 1 - minSum;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution:
  def minStartValue(self, nums: list[int]) -> int:
    summ = 0
    minSum = 0

    for num in nums:
      summ += num
      minSum = min(minSum, summ)

    return 1 - minSum