Minimum Size Subarray Sum(追击指针)
Input:
s = 7, nums = [2,3,1,2,4,3]
Output:
2
Explanation:
the subarray
[4,3]
has the minimal length under the problem constraint.class Solution:
def minSubArrayLen(self, s, nums):
"""
:type s: int
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
n = len(nums)
minLen = n+1
i = j = sum = 0
for i in range(n):
while j < n and sum < s:
sum += nums[j]
j+=1
if sum >= s:
minLen = min(minLen,j-i)
sum-=nums[i]
if minLen == n + 1:
return 0
return minLenLast updated