Skip to content

Commit 97beeb3

Browse files
authored
Create minimum-value-to-get-positive-step-by-step-sum.cpp
1 parent 839f6fc commit 97beeb3

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int minStartValue(vector<int>& nums) {
7+
int min_prefix = 0, prefix = 0;
8+
for (const auto& num : nums) {
9+
prefix += num;
10+
min_prefix = min(min_prefix, prefix);
11+
}
12+
return 1 - min_prefix;
13+
}
14+
};

0 commit comments

Comments
 (0)