# LC 209. Minimum Size Subarray Sum
### [Problem link](https://leetcode.com/problems/minimum-size-subarray-sum/)
###### tags: `leedcode` `medium` `python` `c++` `Sliding Window`
Given an array of positive integers <code>nums</code> and a positive integer <code>target</code>, return the **minimal length** of a **subarray** whose sum is greater than or equal to <code>target</code>. If there is no such subarray, return <code>0</code> instead.
**Example 1:**
```
Input: target = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: The subarray [4,3] has the minimal length under the problem constraint.
```
**Example 2:**
```
Input: target = 4, nums = [1,4,4]
Output: 1
```
**Example 3:**
```
Input: target = 11, nums = [1,1,1,1,1,1,1,1]
Output: 0
```
**Constraints:**
- <code>1 <= target <= 10<sup>9</sup></code>
- <code>1 <= nums.length <= 10<sup>5</sup></code>
- <code>1 <= nums[i] <= 10<sup>4</sup></code>
**Follow up:** If you have figured out the <code>O(n)</code> solution, try coding another solution of which the time complexity is <code>O(n log(n))</code>.
## Solution 1 - Two Pointer
#### Python
```python=
class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
res = float("inf")
total = 0
start = 0
for end, val in enumerate(nums):
total += val
while total >= target:
res = min(res, end - start + 1)
total -= nums[start]
start += 1
return 0 if res == float("inf") else res
```
#### C++
```cpp=
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int n = nums.size();
int ans = n + 1;
int sum = 0;
int left = 0;
for (int right = 0; right < n; right++) {
sum += nums[right];
while (sum >= target) {
ans = min(ans, right - left + 1);
sum -= nums[left];
left++;
}
}
if (ans == n + 1) {
return 0;
}
return ans;
}
};
```
>### Complexity
>| | Time Complexity | Space Complexity |
>| ----------- | --------------- | ---------------- |
>| Solution 1 | O(n) | O(1) |
## Note
x