209. 최소 크기 하위 배열 합계
의문:
n 양의 정수와 양의 정수 s의 배열이 주어지면 합이 s 이상인 연속 하위 배열의 최소 길이를 찾습니다. 하나도 없으면 대신 0을 반환합니다.
예시:
Input: s = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: the subarray [4,3] has the minimal length under the problem constraint.
해결책:
시간 복잡도: O(n)
공간 복잡도: O(1)
var minSubArrayLen = function(s, nums) {
let windowSum = 0
let output = Infinity;
let windowStart = 0;
for (let windowEnd = 0; windowEnd < nums.length; windowEnd++) {
windowSum += nums[windowEnd];
// shrink the window until the windowSum is smaller than s
while (windowSum >= s) {
output = Math.min(output, windowEnd - windowStart + 1);
// subtract the element at the windowStart index
windowSum -= nums[windowStart];
// change windowStart to the next element
windowStart++;
}
}
return output == Infinity ? 0 : output;
};
Reference
이 문제에 관하여(209. 최소 크기 하위 배열 합계), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/cod3pineapple/209-minimum-size-subarray-sum-5g7c텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)