관리 메뉴

Jerry

[day22][leetcode] 209. Minimum Size Subarray Sum 본문

Problem Solving/Algorithm 문제 풀기

[day22][leetcode] 209. Minimum Size Subarray Sum

juicyjerry 2024. 3. 6. 00:37
반응형

SOLUTION 1

/**
 * @param {number} target
 * @param {number[]} nums
 * @return {number}
 */
var minSubArrayLen = function(target, nums) {
    let left = 0;
    let shortedLen = Infinity;
    let sum = 0;

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

        while (sum >= target) {
            shortedLen = Math.min(shortedLen, right - left + 1);
            sum -= nums[left];
            left++;
        }
    }

    console.log(shortedLen);
    return shortedLen === Infinity ? 0 : shortedLen;
};
반응형