Minimum Size Subarray Sum

LeetCode

LeetCode: https://leetcode.com/problems/minimum-size-subarray-sum/

Given an array of positive integers nums and a positive integer target, return the minimal length of a contiguous subarray whose sum is at least target. If there is no such subarray, return 0.

1def minSubArrayLen(target: int, nums: List[int]) -> int:
2 l = 0
3 cur = 0
4 res = float('inf')
5
6 for r in range(len(nums)):
7 cur += nums[r]
8 while cur >= target:
9 res = min(res, r - l + 1)
10 cur -= nums[l]
11 l += 1
12
13 return 0 if res == float('inf') else res
l
2
0
3
1
1
2
2
3
4
4
3
5
cur=0
target=7
best=inf
Step 1 / 5
Step 1:
Initialize window: l=0, cur=0, best=∞. Expand r one step at a time.
Pointers: l=0, r=-1
Focus: default
cur=0target=7best="inf"