leetcode

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

0209.cpp (475B)


      1 class Solution {
      2   public:
      3     int minSubArrayLen(int target, const vector<int> &nums) {
      4         int i = 0, j = 0, sum = 0, res = nums.size() + 1;
      5         while (true) {
      6             if (sum >= target) {
      7                 res = min(res, j - i);
      8                 sum -= nums[i++];
      9             } else {
     10                 if (j == nums.size()) break;
     11                 sum += nums[j++];
     12             }
     13         }
     14         if (res == nums.size() + 1) return 0;
     15         return res;
     16     }
     17 };