Skip to content

Create 11_minValue.cpp #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions 11 November Leetcode Challenge 2021/11_minValue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
int minStartValue(vector<int>& nums) {
// We use "total" for current step-by-step total, "minVal" for minimum
// step-by-step total among all sums. Since we always start with
// startValue = 0, therefore the initial current step-by-step total is 0,
// thus we set "total" and "minVal" be 0.
int minVal = 0;
int total = 0;

// Iterate over the array and get the minimum step-by-step total.
for (int num : nums) {

total += num;
minVal = min(minVal, total);
}

// We have to let the minimum step-by-step total equals to 1,
// by increasing the startValue from 0 to -minVal + 1,
// which is just the minimum startValue we want.
return -minVal + 1;
}
};