/*
* @lc app=leetcode.cn id=209 lang=typescript
*
* [209] 长度最小的子数组
*/
//滑动窗口(思路见代码随想录)
// @lc code=start
function minSubArrayLen(target: number, nums: number[]): number {
let sum = 0,
subLength = 0,
//赋最大初值保证能正常匹配
result = 1000000;
//推进滑动窗口尾部位置
for (let i = 0, j = 0; j < nums.length; j++) {
sum += nums[j];
while (sum >= target) {
//计算子数组长度
subLength = j - i + 1;
//取最短子数组
result = result < subLength ? result : subLength;
//推进滑动窗口起始位置
sum -= nums[i++];
}
}
return result === 1000000 ? 0 : result;
}
// @lc code=end