From acbd97152677408533bb58ac0f2d8564c1ebb4f7 Mon Sep 17 00:00:00 2001 From: hongyiheng Date: Thu, 4 Nov 2021 14:12:09 +0800 Subject: [PATCH 1/4] feat: add solutions to lc problem: No.1413.Minimum Value to Get Positive Step by Step Sum --- .../README.md | 27 +++++++++++++++++-- .../README_EN.md | 27 +++++++++++++++++-- .../Solution.java | 15 +++++++++++ .../Solution.py | 10 +++++++ 4 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.java create mode 100644 solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.py diff --git a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README.md b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README.md index 6638f31532ba4..909f9792be639 100644 --- a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README.md +++ b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README.md @@ -65,7 +65,16 @@ ```python - +class Solution: + def minStartValue(self, nums: List[int]) -> int: + n = len(nums) + pre_sum = [nums[0]] * n + for i in range(1, n): + pre_sum[i] = pre_sum[i - 1] + nums[i] + ans = maxsize + for num in pre_sum: + ans = min(ans, num) + return ans * -1 + 1 if ans <= 0 else 1 ``` ### **Java** @@ -73,7 +82,21 @@ ```java - +class Solution { + public int minStartValue(int[] nums) { + int n = nums.length; + int[] preSum = new int[n]; + preSum[0] = nums[0]; + for (int i = 1; i < n; i++) { + preSum[i] = preSum[i - 1] + nums[i]; + } + int ans = Integer.MAX_VALUE; + for (int num : preSum) { + ans = Math.min(ans, num); + } + return ans < 1 ? ans * -1 + 1 : 1; + } +} ``` ### **...** diff --git a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README_EN.md b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README_EN.md index c8c69a16b7661..95238f1574486 100644 --- a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README_EN.md +++ b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README_EN.md @@ -96,13 +96,36 @@ ### **Python3** ```python - +class Solution: + def minStartValue(self, nums: List[int]) -> int: + n = len(nums) + pre_sum = [nums[0]] * n + for i in range(1, n): + pre_sum[i] = pre_sum[i - 1] + nums[i] + ans = maxsize + for num in pre_sum: + ans = min(ans, num) + return ans * -1 + 1 if ans <= 0 else 1 ``` ### **Java** ```java - +class Solution { + public int minStartValue(int[] nums) { + int n = nums.length; + int[] preSum = new int[n]; + preSum[0] = nums[0]; + for (int i = 1; i < n; i++) { + preSum[i] = preSum[i - 1] + nums[i]; + } + int ans = Integer.MAX_VALUE; + for (int num : preSum) { + ans = Math.min(ans, num); + } + return ans < 1 ? ans * -1 + 1 : 1; + } +} ``` ### **...** diff --git a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.java b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.java new file mode 100644 index 0000000000000..f81f033db1872 --- /dev/null +++ b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.java @@ -0,0 +1,15 @@ +class Solution { + public int minStartValue(int[] nums) { + int n = nums.length; + int[] preSum = new int[n]; + preSum[0] = nums[0]; + for (int i = 1; i < n; i++) { + preSum[i] = preSum[i - 1] + nums[i]; + } + int ans = Integer.MAX_VALUE; + for (int num : preSum) { + ans = Math.min(ans, num); + } + return ans < 1 ? ans * -1 + 1 : 1; + } +} \ No newline at end of file diff --git a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.py b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.py new file mode 100644 index 0000000000000..d69787830849f --- /dev/null +++ b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.py @@ -0,0 +1,10 @@ +class Solution: + def minStartValue(self, nums: List[int]) -> int: + n = len(nums) + pre_sum = [nums[0]] * n + for i in range(1, n): + pre_sum[i] = pre_sum[i - 1] + nums[i] + ans = maxsize + for num in pre_sum: + ans = min(ans, num) + return ans * -1 + 1 if ans <= 0 else 1 \ No newline at end of file From d12f9bdd7152b6cc5b538bea97937385dcc60558 Mon Sep 17 00:00:00 2001 From: hongyiheng Date: Thu, 4 Nov 2021 14:40:55 +0800 Subject: [PATCH 2/4] feat: add solutions to lc problem: No.1413 No.1413.Minimum Value to Get Positive Step by Step Sum --- .../README.md | 26 +++++++------------ .../README_EN.md | 26 +++++++------------ .../Solution.java | 13 ++++------ .../Solution.py | 13 ++++------ 4 files changed, 30 insertions(+), 48 deletions(-) diff --git a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README.md b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README.md index 909f9792be639..0c40c7e259673 100644 --- a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README.md +++ b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README.md @@ -67,14 +67,11 @@ ```python class Solution: def minStartValue(self, nums: List[int]) -> int: - n = len(nums) - pre_sum = [nums[0]] * n - for i in range(1, n): - pre_sum[i] = pre_sum[i - 1] + nums[i] - ans = maxsize - for num in pre_sum: - ans = min(ans, num) - return ans * -1 + 1 if ans <= 0 else 1 + s, t = 0, float('inf') + for num in nums: + s += num + t = min(t, s) + return max(1, 1 - t) ``` ### **Java** @@ -85,16 +82,13 @@ class Solution: class Solution { public int minStartValue(int[] nums) { int n = nums.length; - int[] preSum = new int[n]; - preSum[0] = nums[0]; - for (int i = 1; i < n; i++) { - preSum[i] = preSum[i - 1] + nums[i]; - } + int perSum = 0; int ans = Integer.MAX_VALUE; - for (int num : preSum) { - ans = Math.min(ans, num); + for (int num : nums) { + perSum += num; + ans = Math.min(ans, perSum); } - return ans < 1 ? ans * -1 + 1 : 1; + return ans < 1 ? 1 - ans : 1; } } ``` diff --git a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README_EN.md b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README_EN.md index 95238f1574486..d52a3dd7fa7cb 100644 --- a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README_EN.md +++ b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README_EN.md @@ -98,14 +98,11 @@ ```python class Solution: def minStartValue(self, nums: List[int]) -> int: - n = len(nums) - pre_sum = [nums[0]] * n - for i in range(1, n): - pre_sum[i] = pre_sum[i - 1] + nums[i] - ans = maxsize - for num in pre_sum: - ans = min(ans, num) - return ans * -1 + 1 if ans <= 0 else 1 + s, t = 0, float('inf') + for num in nums: + s += num + t = min(t, s) + return max(1, 1 - t) ``` ### **Java** @@ -114,16 +111,13 @@ class Solution: class Solution { public int minStartValue(int[] nums) { int n = nums.length; - int[] preSum = new int[n]; - preSum[0] = nums[0]; - for (int i = 1; i < n; i++) { - preSum[i] = preSum[i - 1] + nums[i]; - } + int perSum = 0; int ans = Integer.MAX_VALUE; - for (int num : preSum) { - ans = Math.min(ans, num); + for (int num : nums) { + perSum += num; + ans = Math.min(ans, perSum); } - return ans < 1 ? ans * -1 + 1 : 1; + return ans < 1 ? 1 - ans : 1; } } ``` diff --git a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.java b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.java index f81f033db1872..16ec498e3f6b7 100644 --- a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.java +++ b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.java @@ -1,15 +1,12 @@ class Solution { public int minStartValue(int[] nums) { int n = nums.length; - int[] preSum = new int[n]; - preSum[0] = nums[0]; - for (int i = 1; i < n; i++) { - preSum[i] = preSum[i - 1] + nums[i]; - } + int perSum = 0; int ans = Integer.MAX_VALUE; - for (int num : preSum) { - ans = Math.min(ans, num); + for (int num : nums) { + perSum += num; + ans = Math.min(ans, perSum); } - return ans < 1 ? ans * -1 + 1 : 1; + return ans < 1 ? 1 - ans : 1; } } \ No newline at end of file diff --git a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.py b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.py index d69787830849f..61687824a5232 100644 --- a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.py +++ b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.py @@ -1,10 +1,7 @@ class Solution: def minStartValue(self, nums: List[int]) -> int: - n = len(nums) - pre_sum = [nums[0]] * n - for i in range(1, n): - pre_sum[i] = pre_sum[i - 1] + nums[i] - ans = maxsize - for num in pre_sum: - ans = min(ans, num) - return ans * -1 + 1 if ans <= 0 else 1 \ No newline at end of file + s, t = 0, float('inf') + for num in nums: + s += num + t = min(t, s) + return max(1, 1 - t) \ No newline at end of file From a268b10d690fade36a9ac6c5662fe9fcf78ccd96 Mon Sep 17 00:00:00 2001 From: hongyiheng Date: Thu, 4 Nov 2021 14:41:30 +0800 Subject: [PATCH 3/4] feat: add solutions to lc problem: No.1413 No.1413.Minimum Value to Get Positive Step by Step Sum --- .../README.md | 1 - .../README_EN.md | 1 - .../Solution.java | 1 - 3 files changed, 3 deletions(-) diff --git a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README.md b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README.md index 0c40c7e259673..1aac8827221ae 100644 --- a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README.md +++ b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README.md @@ -81,7 +81,6 @@ class Solution: ```java class Solution { public int minStartValue(int[] nums) { - int n = nums.length; int perSum = 0; int ans = Integer.MAX_VALUE; for (int num : nums) { diff --git a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README_EN.md b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README_EN.md index d52a3dd7fa7cb..e2bdd34902717 100644 --- a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README_EN.md +++ b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README_EN.md @@ -110,7 +110,6 @@ class Solution: ```java class Solution { public int minStartValue(int[] nums) { - int n = nums.length; int perSum = 0; int ans = Integer.MAX_VALUE; for (int num : nums) { diff --git a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.java b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.java index 16ec498e3f6b7..25a7e4260248f 100644 --- a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.java +++ b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.java @@ -1,6 +1,5 @@ class Solution { public int minStartValue(int[] nums) { - int n = nums.length; int perSum = 0; int ans = Integer.MAX_VALUE; for (int num : nums) { From eae92f3cb94c27c5af534720e9033318643604ab Mon Sep 17 00:00:00 2001 From: hongyiheng Date: Thu, 4 Nov 2021 14:43:35 +0800 Subject: [PATCH 4/4] feat: add solutions to lc problem: No.1413 No.1413.Minimum Value to Get Positive Step by Step Sum --- .../README.md | 10 +++++----- .../README_EN.md | 10 +++++----- .../Solution.java | 10 +++++----- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README.md b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README.md index 1aac8827221ae..820c649984029 100644 --- a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README.md +++ b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README.md @@ -81,13 +81,13 @@ class Solution: ```java class Solution { public int minStartValue(int[] nums) { - int perSum = 0; - int ans = Integer.MAX_VALUE; + int s = 0; + int t = Integer.MAX_VALUE; for (int num : nums) { - perSum += num; - ans = Math.min(ans, perSum); + s += num; + t = Math.min(t, s); } - return ans < 1 ? 1 - ans : 1; + return Math.max(1, 1 - t); } } ``` diff --git a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README_EN.md b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README_EN.md index e2bdd34902717..ff38f1ff460c2 100644 --- a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README_EN.md +++ b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README_EN.md @@ -110,13 +110,13 @@ class Solution: ```java class Solution { public int minStartValue(int[] nums) { - int perSum = 0; - int ans = Integer.MAX_VALUE; + int s = 0; + int t = Integer.MAX_VALUE; for (int num : nums) { - perSum += num; - ans = Math.min(ans, perSum); + s += num; + t = Math.min(t, s); } - return ans < 1 ? 1 - ans : 1; + return Math.max(1, 1 - t); } } ``` diff --git a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.java b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.java index 25a7e4260248f..ea7e6c7e91404 100644 --- a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.java +++ b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.java @@ -1,11 +1,11 @@ class Solution { public int minStartValue(int[] nums) { - int perSum = 0; - int ans = Integer.MAX_VALUE; + int s = 0; + int t = Integer.MAX_VALUE; for (int num : nums) { - perSum += num; - ans = Math.min(ans, perSum); + s += num; + t = Math.min(t, s); } - return ans < 1 ? 1 - ans : 1; + return Math.max(1, 1 - t); } } \ No newline at end of file