C++ two-pointer O(n) solution.


  • 0
    C
    int minSubArrayLen(int s, vector<int>& nums) {
        int l, r, cum, res = nums.size()+1;
        l = r = cum = 0;
        while ((unsigned int)r < nums.size()) {
            cum += nums[r++];
            while (cum >= s) {
                res = min(res, r-l);
                cum -= nums[l++];
            }
        }
        return res<=nums.size()?res:0;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.