O(n) C++


  • 0
    class Solution {
    public:
     int minSubArrayLen(int s, vector<int>& nums) {
        if(nums.empty()) return 0;
        int n = nums.size();
        int i = 0, j = 0, sum = 0, res = INT_MAX;
        while(j < n){
            while(j < n && sum < s){
                sum += nums[j++];
            }
            while(sum >= s){
                sum -= nums[i++];
            }
            res = min(res, j - i + 1);
        }
        return res > n ? 0 : res;
    }
    

    };


Log in to reply
 

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