From a71be31ed2e3330ae9ff10001ed9805f6ee26c72 Mon Sep 17 00:00:00 2001 From: Fazeel Usmani Date: Thu, 11 Nov 2021 12:11:14 +0530 Subject: [PATCH] Create 11_minValue.cpp --- .../11_minValue.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 11 November Leetcode Challenge 2021/11_minValue.cpp diff --git a/11 November Leetcode Challenge 2021/11_minValue.cpp b/11 November Leetcode Challenge 2021/11_minValue.cpp new file mode 100644 index 0000000..92695f5 --- /dev/null +++ b/11 November Leetcode Challenge 2021/11_minValue.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int minStartValue(vector& 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; + } +};