leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit a81aa3cfaf905d84ac6f27739d1c8367b34d5ff9
parent 90e284b7e569480c147aa97b537f405e3a6da650
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Thu,  6 Jul 2023 17:29:18 +0200

Improved daily problem

Diffstat:
MProblems/0209.cpp | 25++++++++-----------------
1 file changed, 8 insertions(+), 17 deletions(-)

diff --git a/Problems/0209.cpp b/Problems/0209.cpp @@ -1,26 +1,17 @@ class Solution { - int inf = 100001; - public: - int minSubArrayLen(int target, vector<int> &nums) { - int i = 0, j = 0; - int mini = inf; - int sum = nums[0]; - int cnt = 1; + int minSubArrayLen(int target, const vector<int> &nums) { + int i = 0, j = 0, sum = 0, res = nums.size() + 1; while (true) { if (sum >= target) { - do { - mini = min(mini, cnt); - sum -= nums[i++]; - cnt--; - } while (i < j && sum >= target); + res = min(res, j - i); + sum -= nums[i++]; } else { - if (++j >= nums.size()) break; - sum += nums[j]; - cnt++; + if (j == nums.size()) break; + sum += nums[j++]; } } - if (mini == inf) return 0; - return mini; + if (res == nums.size() + 1) return 0; + return res; } };