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

209. Minimum Size Subarray Sum

时间:2024-06-16 12:02:30浏览次数:23  
标签:return target nums 209 Sum int result Subarray sum

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

Constraints:

  • 1 <= target <= 109
  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 104

Follow up: If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log(n)).

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int i=0;
        int result=INT32_MAX;
        int sum=0;
        int subLength=0;
        for(int j=0;j<nums.size();j++){
            sum+=nums[j];
            while(sum>=target){
                subLength=j-i+1;
                result=min(result,subLength);
                sum=sum-nums[i++];
            }
        }
        return result==INT32_MAX?0:result;
    }
};

注意:滑动窗口

        1.i表示起始位置,j表示终止位置

        2.sum>=target要用while,而不是if

        3.return的时候要多加上一个二元判断

标签:return,target,nums,209,Sum,int,result,Subarray,sum
From: https://blog.csdn.net/2301_80161204/article/details/139717649

相关文章

  • 【LeetCode最详尽解答】15-三数之和 3sum
    欢迎收藏Star我的MachineLearningBlog:https://github.com/purepisces/Wenqing-Machine_Learning_Blog。如果收藏star,有问题可以随时与我交流,谢谢大家!链接:15-三数之和直觉示例:输入:nums=[-1,0,1,2,-1,-4]输出:[[-1,-1,2],[-1,0,1]]解释:nums[......
  • square869120Contest #3 G Sum of Fibonacci Sequence
    洛谷传送门AtCoder传送门特判\(n=1\)。将\(n,m\)都减\(1\),答案即为\[[x^m]\frac{1}{(1-x-x^2)(1-x)^n}\]若能把这个分式拆成\(\frac{A(x)}{(1-x)^n}+\frac{B(x)}{1-x-x^2}\)的形式,其中\(\degA(x)\len-1,\degB(x)\le1\),那么答案就是好算的。......
  • JDK8新特性【接口新特征、lambda语法、Supplier、Consumer、Function、Predicate】
    目录一、关于接口的新特性1.1jdk1.8之前的接口重要特性1.2JDK8以后代码演示1.3总结通过代码演示发现作用二、Lambda表达式[重点]2.1将匿名内部类写法改写为lambda写法2.2语法特点能够写成lambda形式的的前提语法特征代码演示深入理解lambda2.3总结三、函数......
  • ABC348E Minimize Sum of Distances 题解
    ABC348EMinimizeSumofDistances题目大意给定一棵共\(n\)个节点的树,第\(i\)个点的权重为\(c_i\)。定义\(f(x)\)表示树上所有点到节点\(x\)的距离乘上权重,即\(f(x)=\sum\limits_{i=1}^n(c_i\timesdis(x,i))\)。求\(\min\limits_{u=1}^nf(u)\)。Solve一眼换根......
  • ABC 321 F #(subset sum = K) with Add and Erase
    题意有一个箱子,每次可以向里面添加或者拿走一个数,问每次操作过后,任选箱子里的数相加,总和等于k的方案数是多少。思路萌新算是学到新东西了,这其实是个可撤销背包的板题。我们先考虑一个问题:对于普通计数类dp,我们现在禁用某一个数i,我们现在想知道某一个数j有多少种方式表示(即dp......
  • 1. Two Sum Go实现
    在数组中找到2个数之和等于给定值的数字,结果返回2个数字在数组中的下标。1.解法1时间复杂度O(n^2)直接两次遍历所有节点,进行求和比较代码如下:functwoSum(nums[]int,targetint)[]int{ res:=make([]int,2,2) fori:=0;i<len(nums);i++{ forj:=i+1;j<len......
  • C. Minimizing the Sum
    原题链接题解1.任何一个数,只能覆盖一次2.把被覆盖的数字具象化,那么最终数组一定是由若干个有颜色的区间(被覆盖)和无颜色区间(没有被覆盖)组成3.这里就是状态的巧妙之处了,已知我们要求\(n\)个数里最多\(k\)个数被覆盖的最小和,那么这\(k\)个数里,一定存在末尾连续\(j\)个数......
  • 力控算法每日一练:209. 长度最小的子数组(java)
    给定一个含有 n 个正整数的数组和一个正整数 target 。找出该数组中满足其总和大于等于 target 的长度最小的 子数组 [numsl,numsl+1,...,numsr-1,numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0 。classSolution{publicintminSubArrayL......
  • Summary:《Adversarial Machine Learning in Image Classification: A Survey Towards
    Note“TaxonomyofAdversarialImages”(Machado等,2023,p.5)(pdf)扰动范围(PerturbationScope):个体扰动(Individual-scopedperturbations):为每个输入图像单独生成的扰动。通用扰动(Universal-scopedperturbations):独立于任何输入样本生成的扰动,可应用于任何合......
  • ICS3U – Summative A
    ICS3U–SummativeAssignmentMETHODSANDARRAYSSearchingforSugar:SugartheSlothislostintheJungle(oryoucouldthinkofitasagridwithrowsandcolumns)andyouwillneedhelphimescape–whileavoidingtheroamingpredatorsthatareafter......