首页 > 其他分享 >209. Minimum Size Subarray Sum

209. Minimum Size Subarray Sum

时间:2023-03-07 13:07:13浏览次数:38  
标签:min 209 Sum len int Subarray array INT sum

#题目
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn’t one, return 0 instead.

For example, given the array [2,3,1,2,4,3] and s = 7,
the subarray [4,3] has the minimal length under the problem constraint.
#思路
本题给出一个O(n)的思路。这道题目需要维护两个数组索引i,j。通过不断地增加i或增加j来判断是否有满足条件的值。具体思路看代码实现。
#代码

class Solution {
public:
    int minSubArrayLen(int s, vector<int>& nums) {
        int sum=0,i=0,j=0,min_len=INT_MAX;
        while(j<nums.size()){
            sum+=nums[j++];
            while(sum>=s)
            {
                min_len = min(min_len,j-i);
                sum-=nums[i++];
            }
        }
        return min_len==INT_MAX?0:min_len;     
    }
};

标签:min,209,Sum,len,int,Subarray,array,INT,sum
From: https://blog.51cto.com/u_15996214/6105904

相关文章

  • 560. Subarray Sum Equals K
    560.SubarraySumEqualsK标签(空格分隔):leetcodearraymedium题目Givenanarrayofintegersandanintegerk,youneedtofindthetotalnumberofcont......
  • 228. Summary Ranges
    #题目Givenasortedintegerarraywithoutduplicates,returnthesummaryofitsranges.Example1:Input:[0,1,2,4,5,7]Output:[“0->2”,“4->5”,“7”]......
  • 713. Subarray Product Less Than K
    713.SubarrayProductLessThanK题目Youraregivenanarrayofpositiveintegersnums.Countandprintthenumberof(contiguous)subarrayswherethe......
  • 152. Maximum Product Subarray
    #题目Findthecontiguoussubarraywithinanarray(containingatleastonenumber)whichhasthelargestproduct.Forexample,giventhearray[2,3,-2,4],t......
  • golang 升级 1.16.3 之后,编译报错 missing go.sum entry for module providing packag
    问题现象在开发机上升级到了最新golang1.16.3版本,在为一个基于golang1.13的历史项目添加excel依赖包后gogetgithub.com/360EntSecGroup-Skylar/excelize/v2......
  • 容斥定理 AtCoder——FizzBuzz Sum Hard
    题目传送门ProblemStatementFindthesumofintegersbetween 1 and N(inclusive)thatarenotmultiplesof Aor B.Constraints1≤N,A,B≤109 Allvalue......
  • 209. 长度最小的子数组 (Medium)
    问题描述209.长度最小的子数组(Medium)给定一个含有n个正整数的数组和一个正整数target。找出该数组中满足其和≥target的长度最小的连续子数组[numsₗ,num......
  • 好用的数据校验&修复工具gt-checksum开源啦
    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源。GreatSQL是MySQL的国产分支版本,使用上与MySQL一致。作者:GreatSQL社区文章来源:GreatSQL社区原......
  • [ABC273G] Row Column Sums 2 题解
    [ABC273G]RowColumnSums2Solution目录[ABC273G]RowColumnSums2Solution更好的阅读体验戳此进入题面SolutionCodeUPD更好的阅读体验戳此进入题面给定$n$,给......
  • D. Maximum Subarray
    D.MaximumSubarrayYouaregivenanarray$a_1,a_2,\dots,a_n$,consistingof$n$integers.Youarealsogiventwointegers$k$and$x$.Youhavetoperform......