Leetcode - 209. Minimum Size Subarray Sum

/** * @param {number} target * @param {number[]} nums * @return {number} */ var minSubArrayLen = function (target, nums) { let minLength = Infinity; let p1 = 0; let sum = 0; for (let p2 = 0; p2 = target) { minLength = Math.min(minLength, p2 - p1 + 1); sum -= nums[p1]; p1++; } } return minLength === Infinity ? 0 : minLength; };

Mar 2, 2025 - 16:33
 0
Leetcode - 209. Minimum Size Subarray Sum
/**
 * @param {number} target
 * @param {number[]} nums
 * @return {number}
 */
var minSubArrayLen = function (target, nums) {

    let minLength = Infinity;
    let p1 = 0;
    let sum = 0;

    for (let p2 = 0; p2 < nums.length; p2++) {
        sum += nums[p2];

        while (sum >= target) {
            minLength = Math.min(minLength, p2 - p1 + 1);
            sum -= nums[p1];
            p1++;
        }
    }

    return minLength === Infinity ? 0 : minLength;
};