给定一个含有 n 个正整数的数组和一个正整数 target 。
找出该数组中满足其和 ≥ target 的长度最小的 连续子数组 [numsl, numsl+1, …, numsr-1, numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0 。
示例 1:
输入:target = 7, nums = [2,3,1,2,4,3]
输出:2
解释:子数组 [4,3] 是该条件下的长度最小的子数组。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/2VG8Kg
class Solution(object):
def minSubArrayLen(self, target, nums):
"""
:type target: int
:type nums: List[int]
:rtype: int
"""
left, right = 0, 0
length = 10**5 + 1
lens = len(nums)
num = 0
while right < lens:
num += nums[right]
while left <= right and num >= target:
length = min(length, right-left+1)
num -= nums[left]
left += 1
right += 1
return length if length<10**5 else 0