Minimum Size Subarray Sum
franklinqin0 ArrayTwo PointersBinary Search
# Solution
# Iterate right
def minSubArrayLen(self, s: int, nums: List[int]) -> int:
n = len(nums)
csum = 0
left = 0
res = sys.maxsize
for right in range(n):
csum += nums[right]
while csum >= s and left < n:
res = min(res, right - left + 1)
csum -= nums[left]
left += 1
if res == sys.maxsize:
return 0
else:
return res
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Iterate left
def minSubArrayLen(self, s: int, nums: List[int]) -> int:
n = len(nums)
csum = 0
right = 0
res = sys.maxsize
for left in range(n):
while csum < s and right < n:
csum += nums[right]
right += 1
if csum >= s:
res = min(res, right - left)
csum -= nums[left]
if res == sys.maxsize:
return 0
else:
return res
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17