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.

题目大意:给定一个非负数组,和一个指定值,找出最小子数组长度使得这个子数组的和大于指定的值。

解题思路:采用滚动计算的思路,类似rolling hash的思想,首先累加数组元素,当大于指定元素时——>执行(从这个子数组的第一个数开始,循环减去这些数),遍历一遍,得到最小子数组长度。

    public int minSubArrayLen(int s, int[] nums) {
        if (nums == null||nums.length == 0) {
            return 0;
        }
        int min = 0,pos=0,len=Integer.MAX_VALUE;
        for (int i =0; i<nums.length; i++ ) {
            min+=nums[i];
            while(min>=s){
                if (len>i-pos+1) {
                    len=i-pos+1;
                }
                min-=nums[pos];
                pos++;
            }
        }
        return len==Integer.MAX_VALUE?0:len;
    }
时间: 2024-10-08 19:12:47

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 m

[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,就把左端向右移动一位,否则更新当前的最小连续子数组长度. 代码