当前位置: 首页> 文旅> 美景 > 成为架构师有多难_杭州互联网网页设计公司_上百度推广的网站要多少钱_推广产品

成为架构师有多难_杭州互联网网页设计公司_上百度推广的网站要多少钱_推广产品

时间:2025/7/8 15:28:52来源:https://blog.csdn.net/weixin_44245188/article/details/143431715 浏览次数:1次
成为架构师有多难_杭州互联网网页设计公司_上百度推广的网站要多少钱_推广产品

Given an array of positive integers nums and a positive integer target, return the minimal length of a
subarray
whose sum is greater than or equal to target. If there is no such subarray, return 0 instead.

Example 1:

Input: target = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: The subarray [4,3] has the minimal length under the problem constraint.

Example 2:

Input: target = 4, nums = [1,4,4]
Output: 1

Example 3:

Input: target = 11, nums = [1,1,1,1,1,1,1,1]
Output: 0

思路:思路其实很简单,就是维护一个动态的滑动窗口,我感觉大家都会这么想吧。看了题解后,发现似乎官方都不是一开始用的滑动窗口!我们可以通过一个窗口来维持里面的数的和始终小于target,不断往里面增加数字,大于等于target时,就需要移动左边的指针,不断缩减,因为我们的目标是求得最短的,所以要是的窗口的区间足够小。

class Solution:def minSubArrayLen(self, s: int, nums: List[int]) -> int:n=len(nums)l=0res=float("inf")tmp=0for r in range(n):tmp+=nums[r]while(tmp>=s):res=min(res,r-l+1)tmp-=nums[l]l+=1return res if(res!=float("inf")) else 0
关键字:成为架构师有多难_杭州互联网网页设计公司_上百度推广的网站要多少钱_推广产品

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: