Minimum Size Subarray Sum -- leetcode

题目描述:

Given an array of n positive integers and a positive integer s, find the minimal length of a 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.

意思是讲给定一组整数数组nums和一个数字s,求的nums数组中最短长度的连续数字和,使得该和大于等于s。

比如上面实例数组 2 ,3,1,2,4,3这一个数组中,大于等于7的最短连续为 3 ,4 那么 minimal为2

解题思路

定义一个start ,表示序列的其实点,初始值为0

定义一个tempresult,用来累积连续序列和,初始值为0

定义一个minlength,用来表示最短序列,初始值为INT-MAX

  • 一个循环,首先从数组开始处i=0,此时start=0,往后累积和,tempresult+=nums[i]
  • 判断累积和和s的大小,如果大于等于s,则进行如下操作
    - 先计算当前minlength=min(minlength,i-satrt+1)
    
    -然后将tempresult的值减去这个序列的nums[start],起始点start++再次计算minlength(缩短整个序列),然后继续判断该值跟s的大小,直到减去的值使得tempresult的值小于s
    
  • 如果小于s,则继续累积和,直到满足和大于等于s
  • 注意,还有可能存在不存在序列的,就是整个序列和都相加都小于s,这是就需要判断(程序返回时候i-start==nums.size()&&tempresult小于s

拿上面的数组列子进行讲解nums= 2 ,3,1,2,4,3,s=7

首先 从2开始加,一直加到 2,3,1,2此时tempresult=8,大于等于7,先求的但当前minlength=4,然后缩短序列 start++,序列为3,1,2,相应的tempresult=6,小于7,则继续往后面累积和,3,1,2,4,然后缩短序列,变成,1,2,4,start++,此时minlength=3,然后继续缩短,2,4小于7,然后往后面继续累加,2,4,3,大于7,缩短,4,3,大于等于7,minlength=2,,到最后了,结束。

代码如下

class Solution {
public:
    int minSubArrayLen(int s, vector<int>& nums) {

            int i,start,minlength,tempresult;

    minlength=INT_MAX;
    start=0;
    tempresult=0;
    for(i=start;i<nums.size();i++)
    {
        tempresult+=nums[i];

        if(tempresult>=s)
        {
            minlength=min(minlength,i-start+1);

            tempresult-=nums[start];
            start++;
            while(tempresult>=s)
            {

                minlength=min(minlength,i-start+1);
                tempresult-=nums[start];
                start++;
            }
        }

    }
    if(i-start==nums.size()&&tempresult<s)
        return 0;

    return minlength;
    }
};
时间: 2024-08-09 21:58:55

Minimum Size Subarray Sum -- leetcode的相关文章

Minimum Size Subarray Sum —— LeetCode

Given an array of n positive integers and a positive integer s, find the minimal length of a 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

[LeetCode] Minimum Size Subarray Sum 解题思路

Given an array of n positive integers and a positive integer s, find the minimal length of a 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

【LeetCode】209. Minimum Size Subarray Sum

Minimum Size Subarray Sum Given an array of n positive integers and a positive integer s, find the minimal length of a 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 subar

leetcode_209题——Minimum Size Subarray Sum(两个指针)

Minimum Size Subarray Sum Total Accepted: 10318 Total Submissions: 44504My Submissions Question Solution Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, r

[LeetCode] 209. Minimum Size Subarray 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. Example: Input: s = 7, nums = [2,3,1,2,4,3] Output: 2 Explanation: the subarr

【leetcode】Minimum Size Subarray Sum(middle)

Given an array of n positive integers and a positive integer s, find the minimal length of a 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

LeetCode OJ 209. Minimum Size Subarray Sum

Given an array of n positive integers and a positive integer s, find the minimal length of a 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

LeetCode 209. Minimum Size Subarray Sum (O(n)实现)

动态规划: len[i]: - 若存在begin使得sum(nums.begin()+begin, nums.begin()+i+1)>=s且sum(nums.begin()+begin-1, nums.begin()+i+1)<s,那么len[i] = i - begin + 1 (这串数字的长度) - 反之,len[i] = len[i - 1] + 1; sum[i]: - 若存在上述begin, sum = sum(nums.begin()+begin, nums.begin()+i+

LeetCode 209. 长度最小的子数组(Minimum Size Subarray Sum)

题目描述 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 0. 示例: 输入: s = 7, nums = [2,3,1,2,4,3] 输出: 2 解释: 子数组 [4,3] 是该条件下的长度最小的连续子数组. 解题思路 记录当前的连续子数组和,若大于等于s,则以当前子数组的最左端为基准向后遍历,若去掉此数后当前连续子数组和仍大于等于s,就把左端向右移动一位,否则更新当前的最小连续子数组长度. 代码