Leetcode 209 Solution

This article provides solution to leetcode question 209 (minimum-size-subarray-sum)

https://leetcode.com/problems/minimum-size-subarray-sum

Solution

class Solution {
public:
    int minSubArrayLen(int s, vector<int>& nums) {
        int curr_sum = 0;
        int len = INT_MAX;
        int l = 0;
        int r = 0;

        while (r < nums.size())
        {
            curr_sum += nums[r++];

            while (curr_sum - nums[l] >= s)
                curr_sum -= nums[l++];

            if (curr_sum >= s)
                len = min(len, r - l);
        }

        return len == INT_MAX ? 0 : len;
    }
};