C++ Two-pointer O(N) solution, a single while loop


  • 0
        int minSubArrayLen(int s, vector<int>& nums) {
            int start = 0, end = 0, minLen = INT_MAX, sum = 0;
            
            while (end <= nums.size()) {
                if (sum >= s) {
                    minLen = min(minLen, end-start);
                    sum -= nums[start++];
                }
                else if (end < nums.size()) sum += nums[end++];
                else break;
            }
            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.