Simple c++ O(n) solution, easy to understand


  • -1
    I
    int minSubArrayLen(int s, vector<int>& nums) {
        int minLen = INT_MAX, curSum = 0, left = 0;
        for (int right = 0; right < nums.size(); right++) {
            curSum += nums[right];
            if (curSum >= s) {
                while (left < right && curSum - nums[left] >= s) {
                    curSum -= nums[left++];
                }
                minLen = min(minLen, right - left + 1);
            }
        }
        return minLen == INT_MAX ? 0 : minLen;
    }

Log in to reply
 

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