10-line C++ O(n) Solution


  • 6
    L

    Just the windowing technique.

    class Solution {
    public:
        int minSubArrayLen(int s, vector<int>& nums) {
            int sum=0,front=0,back=0,res=INT_MAX;
            while(front<nums.size()||sum>=s) {
                if(sum<s&&front<nums.size())
                    sum+=nums[front++];
                if(sum>=s) {
                    res=min(res,front-back);
                    sum-=nums[back++];
                }
            } 
            return res==INT_MAX?0:res;
        }
    };

  • 0
    K

    what if s == INT_MAX and nums size is INT_MAX and contains only 1s?


Log in to reply
 

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