clueless coding // TODO: be smarter

LeetCode Minimum Size Subarray Sum: CPP Solution



TL;DR Code Solution


int minSubArrayLen(int s, vector<int>& nums) {

    int left = 0;
    int right = 0;
    int result = INT_MAX;
    int curSum = 0;
    while (left < nums.size() && right < nums.size()) {

        while (curSum < s && right < nums.size()) {
            curSum += nums[right];
            right++;
        }

        while (curSum >= s) {
            result = min(result, right - left);
            curSum -= nums[left];
            left++;
        }


    }

    return result == INT_MAX ? 0 : result;
}