Skip to content

Commit 43d578c

Browse files
add 1413
1 parent 7eb5e92 commit 43d578c

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1413|[Minimum Value to Get Positive Step by Step Sum](https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1413.java) | |Easy|Array|
1112
|1410|[HTML Entity Parser](https://leetcode.com/problems/html-entity-parser/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1410.java) | |Medium|String, Stack|
1213
|1409|[Queries on a Permutation With Key](https://leetcode.com/problems/queries-on-a-permutation-with-key/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1409.java) | |Medium|Array|
1314
|1408|[String Matching in an Array](https://leetcode.com/problems/string-matching-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1408.java) | |Easy|String|
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1413. Minimum Value to Get Positive Step by Step Sum
5+
*
6+
* Given an array of integers nums, you start with an initial positive value startValue.
7+
* In each iteration, you calculate the step by step sum of startValue plus elements in nums (from left to right).
8+
* Return the minimum positive value of startValue such that the step by step sum is never less than 1.
9+
*
10+
* Example 1:
11+
* Input: nums = [-3,2,-3,4,2]
12+
* Output: 5
13+
* Explanation: If you choose startValue = 4, in the third iteration your step by step sum is less than 1.
14+
* step by step sum
15+
* startValue = 4 | startValue = 5 | nums
16+
* (4 -3 ) = 1 | (5 -3 ) = 2 | -3
17+
* (1 +2 ) = 3 | (2 +2 ) = 4 | 2
18+
* (3 -3 ) = 0 | (4 -3 ) = 1 | -3
19+
* (0 +4 ) = 4 | (1 +4 ) = 5 | 4
20+
* (4 +2 ) = 6 | (5 +2 ) = 7 | 2
21+
*
22+
* Example 2:
23+
* Input: nums = [1,2]
24+
* Output: 1
25+
* Explanation: Minimum start value should be positive.
26+
*
27+
* Example 3:
28+
* Input: nums = [1,-2,-3]
29+
* Output: 5
30+
*
31+
* Constraints:
32+
* 1 <= nums.length <= 100
33+
* -100 <= nums[i] <= 100
34+
* */
35+
public class _1413 {
36+
public static class Solution1 {
37+
public int minStartValue(int[] nums) {
38+
int min = Integer.MAX_VALUE;
39+
int sum = 0;
40+
for (int num : nums) {
41+
sum += num;
42+
min = Math.min(sum, min);
43+
}
44+
return min > 0 ? 1 : Math.abs(min) + 1;
45+
}
46+
}
47+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1413;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1413Test {
10+
private static _1413.Solution1 solution1;
11+
private static int[] nums;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1413.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
nums = new int[]{-3, 2, -3, 4, 2};
21+
assertEquals(5, solution1.minStartValue(nums));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
nums = new int[]{1, 2};
27+
assertEquals(1, solution1.minStartValue(nums));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
nums = new int[]{1, -2, -3};
33+
assertEquals(5, solution1.minStartValue(nums));
34+
}
35+
36+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy