Skip to content

Commit 6937e3d

Browse files
committed
added Minimum Value to Get Positive Step by Step Sum (easy)
1 parent 88ebb4c commit 6937e3d

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

Easy/MinValStepSum/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
# Minimum Value to Get Positive Step by Step Sum
3+
[Leetcode Link](https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/)
4+
5+
## Problem:
6+
7+
Given an array of integers `nums`, you start with an initial **positive** value *startValue*.
8+
9+
In each iteration, you calculate the step by step sum of *startValue* plus elements in `nums` (from left to right).
10+
11+
Return the minimum **positive** value of *startValue* such that the step by step sum is never less than 1.
12+
13+
14+
15+
## Example:
16+
17+
```
18+
Input: nums = [-3,2,-3,4,2]
19+
Output: 5
20+
Explanation: If you choose startValue = 4, in the third iteration your step by step sum is less than 1.
21+
step by step sum
22+
startValue = 4 | startValue = 5 | nums
23+
(4 -3 ) = 1 | (5 -3 ) = 2 | -3
24+
(1 +2 ) = 3 | (2 +2 ) = 4 | 2
25+
(3 -3 ) = 0 | (4 -3 ) = 1 | -3
26+
(0 +4 ) = 4 | (1 +4 ) = 5 | 4
27+
(4 +2 ) = 6 | (5 +2 ) = 7 | 2
28+
```
29+
```
30+
Input: nums = [1,2]
31+
Output: 1
32+
Explanation: Minimum start value should be positive.
33+
```
34+
```
35+
Input: nums = [1,-2,-3]
36+
Output: 5
37+
```
38+
39+
## Note:
40+
41+
- `1 <= nums.length <= 100`
42+
- `-100 <= nums[i] <= 100`

Easy/MinValStepSum/Solution.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Let positive start value be X
2+
// For each num in nums:
3+
// the step sum value is X + num[0], X + num[0] + num[1]...
4+
// so for each num, the sum value is X + SUM(num so far)
5+
// we just need to guarantee that sum value is positive for any SUM(num so far)
6+
7+
package Easy.MinValStepSum;
8+
9+
/**
10+
* Solution
11+
*/
12+
public class Solution {
13+
14+
public int minStartValue(int[] nums) {
15+
int startVal = 1;
16+
int sum = 0;
17+
for (int num : nums) {
18+
sum += num;
19+
if (startVal + sum < 1) {
20+
startVal = 1 - sum;
21+
}
22+
}
23+
return startVal;
24+
}
25+
26+
}

Easy/MinValStepSum/TestDriver.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package Easy.MinValStepSum;
2+
3+
import java.util.Arrays;
4+
5+
public class TestDriver {
6+
7+
public static void main(String[] args) {
8+
Solution sol = new Solution();
9+
int nums1[] = {-3, 2, -3, 4, 2};
10+
System.out.println("Input: nums = " + Arrays.toString(nums1));
11+
System.out.println("Output: " + sol.minStartValue(nums1));
12+
System.out.println();
13+
14+
int nums2[] = {1, 2};
15+
System.out.println("Input: nums = " + Arrays.toString(nums2));
16+
System.out.println("Output: " + sol.minStartValue(nums2));
17+
System.out.println();
18+
19+
int nums3[] = {1, -2, -3};
20+
System.out.println("Input: nums = " + Arrays.toString(nums3));
21+
System.out.println("Output: " + sol.minStartValue(nums3));
22+
System.out.println();
23+
}
24+
25+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Languages used: Java and Python
2121
- [Find Common Characters](Easy/FindCommonCharacters)
2222
- [Third Maximum Number](Easy/ThirdMaximumNumber)
2323
- [String Matching in an Array](Easy/StringMatchingInArray)
24+
- [Minimum Value to Get Positive Step by Step Sum](Easy/MinValStepSum)
2425
- Medium
2526
- [Minimum Add to Make Parentheses Valid](Medium/MinimumAddtoMakeParenthesesValid)
2627
- [Distribute Coins in Binary Tree](Medium/DistributionCoinsInBinaryTree)

0 commit comments

Comments
 (0)