sliding window C++

mail@pastecode.io avatar
unknown
c_cpp
10 days ago
476 B
0
Indexable
Never
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int i=0,j=0;
        int sum=0;
        int mini=INT_MAX;

        while(j<nums.size())
        {
            sum+=nums[j];
            while(sum>=target)
            {
                mini=min(mini,j-i+1);
                sum=sum-nums[i];
                i++;
            }
            j++;
        } 
        if(mini==INT_MAX)  
        return 0;
        
        return mini;
    }
};
Leave a Comment