Sharing my 4ms C++ solution


  • 0
    T
    class Solution {
    public:
        int minSubArrayLen(int s, vector<int>& nums) {
            int n = nums.size();
            int result = n + 1;
            int left = 0;
            int right = 0;
            int sum = 0;
            while(right < n)
            {
                sum = sum + nums[right];
                right = right + 1;
                while(sum >= s)
                {
                    result = min(result, right-left);
                    sum = sum - nums[left];
                    left = left + 1;
                }
            }
            
            if(result == n+1)
                return 0;
            else 
                return result;
        }
    };

Log in to reply
 

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