探索数组和字符串
给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组。如果不存在符合条件的连续子数组,返回 0。
示例:
输入: s = 7, nums = [2,3,1,2,4,3]
输出: 2
解释: 子数组 [4,3] 是该条件下的长度最小的连续子数组。
class Solution:
def minSubArrayLen(self,s: int, nums) -> int:
n = len(nums)
L, R = 0, 0 # 滑动窗口左右边界
total = 0
res = n + 1
while R < n:
total += nums[R]
while total >= s:
res = min(res, R - L + 1)
total -= nums[L]
L += 1
R += 1
return res if res < n + 1 else 0