---
# System prepended metadata

title: 209-Minimum Size Subarray Sum
tags: [Medium]

---

# 209-Minimum Size Subarray Sum
###### tags: `Medium`
## Question
https://leetcode.com/problems/minimum-size-subarray-sum/

## Issue
是不是有點像backtracking跟two pointer結合?
還是說沒用到遞迴不算backtracking?

## Key
透過sliding window技巧，使用兩個變數紀錄subarray的頭跟尾，先將尾巴已經移動到符合目標上限的位置，這樣可以避免窮舉出超出目標的subarray，接下來開始將頭部右移直到出現最短的subarray

## Reference
## Sliding window Solution
```cpp=
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int windowSum = 0;
        int windowStart = 0;
        int n = nums.size();
        int minWinSize = INT_MAX;
        for(int i = 0; i < n; i++)
        {
            windowSum += nums[i];
            while(windowSum >= target)
            {
                minWinSize = min(minWinSize, i-windowStart+1);
                windowSum -= nums[windowStart];
                windowStart++;
            }
        }
        return minWinSize == INT_MAX? 0: minWinSize;
    }
};
```

