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 length under the problem constraint.

click to show more practice.

More practice:

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

思路:

1.找到第一符合条件的长度

2.先加上后面的一个元素

3.如果减去前面的一个元素后sum小于target,转到2

3.减去前面的n个元素后符合条件&&减去前面的n+1个元素后不符合条件,获得一个新的长度,跟最小长度相比,小于minLen,更新minLen=newLen

4.若果start<end&&end<nums.length,转至2

代码:

public int minSubArrayLen(int s, int[] nums) {
        if(nums==null||nums.length==0)
            return 0;
        int start=0,end=-1;
        int sum=0,min=0;
        int temp=0;
        while(sum<s&&end<nums.length-1)//找到第一符合条件的长度
        	sum+=nums[++end];
        if(sum<s)
            return 0;
        min=end-start+1;
        while(start<end&&end<nums.length)
        {
        	if(end+1<nums.length)
        		sum+=nums[++end];
            if(sum-nums[start]<s)
            {
            	if(end==nums.length-1)//已经到达最后
                	break;
            	else
            		continue;//新加元素后减去开始元素后小于s
            }
            while(start<end&&sum-nums[start]>=s)////新加元素后减去开始元素后不小于s
                sum-=nums[start++];
            temp=end-start+1;
            min=min<temp?min:temp;
        }
        return min;
    }
时间: 2024-08-28 22:05:18

leetcode_Minimum Size Subarray Sum的相关文章

[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 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

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] Maximum Size Subarray Sum Equals k 最大子数组之和为k

Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead. Example 1: Given nums = [1, -1, 5, -2, 3], k = 3, return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is th

[Locked] Maximum Size Subarray Sum Equals k

Example 1: Given nums = [1, -1, 5, -2, 3], k = 3,return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is the longest) Example 2: Given nums = [-2, -1, 2, 1], k = 1,return 2. (because the subarray [-1, 2] sums to 1 and is the longest) Follow U

【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

lintcode-medium-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 -1 instead. Example Given the array [2,3,1,2,4,3] and s = 7, the subarray [4,3] has the minimal len

LeetCode 325. Maximum Size Subarray Sum Equals k

类似sum的题都可以想一想可不可以把sum依次求出来,然后 [i+1,j] 的sum 直接用 sum[j]-sum[i]来求. 由于这道题有负数存在,没办法用sliding window来做,只能另辟蹊径. 用一个hashtable保存到目前为止的sum所对应的index,这样之后知道寻找 sum-k 在不在hashtable里就行了.如果遇到sum相同的情况,不要更新hashtable,因为更新了,subarray的size必然是减小的. class Solution { public: in

[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