Skip to main content

209. Minimum Size Subarray Sum

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

Python

class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
# Can be any of larger number which not possible from the answer
min_length = len(nums)+1
total = 0
start, end = 0, 0

while end < len(nums):
# print("window", nums[start:end+1])
total += nums[end]

while (total >= target):
min_length = min(min_length, end-start+1)
total -= nums[start]
start += 1
end += 1

return min_length if min_length < len(nums)+1 else 0