Clear O(n) 10 lines C++ Solution


  • 0
    S
    class Solution {
    public:
        int minSubArrayLen(int s, vector<int>& nums) {
            int m = INT_MAX, l = 0, i, sum = 0;
            for (i = 0; i < nums.size(); ++i)
            {
                sum += nums[i];
                while(sum - nums[l] >= s)
                    sum -= nums[l++];
                if (sum >= s && i - l + 1 < m) m = i - l + 1;
            }
            if (sum < s) return 0;
            return m;
        }
    };

Log in to reply
 

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