[leetcode] 209. Minimum Size Subarray Sum

转载 2016年08月29日 12:42:55

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).

解法一:

class Solution {
public:
    int minSubArrayLen(int s, vector<int>& nums) {
        if(nums.empty()) return 0;
        int left =0, right =0, sum = 0, len = nums.size();
        int res = len+1;
        
        while(right<len){
            while(sum<s && right<len){
                sum += nums[right++];
            }
            
            while(sum>=s){
                sum -= nums[left++];
                res = min(res,right-left+1);
            }
        }
        
        return res==len+1?0:res;
        
    }
};

解法二:

建立一个sum,每一个sum[i]表示num[0,i-1]的和。那么从每一个数字num[i],在sum[i+1,end]找到第一个比num[i]+s还大的数,就是从num[i]开始和大于s的最小区间。

class Solution {
public:
    int minSubArrayLen(int s, vector<int>& nums) {
        if(nums.empty()) return 0;
        int len = nums.size();
        int res = len+1;
        
        vector<int> sum(len+1,0); 
        for(int i=1; i<len+1; i++) sum[i] = sum[i-1]+nums[i-1];
        
        for(int i=0; i<len+1; i++){
            int right = searchRight(sum,i+1,len,sum[i]+s);
            if(right==len+1) break;
            if(res>right-i) res = right-i;
        }
        
        return res==len+1?0:res;
        
    }
    
    int searchRight(vector<int>& sum, int left, int right, int target){
        
        while(left<=right){
            int mid = (left+right)/2;
            if(sum[mid]>=target) right = mid -1;
            else left = mid+1;
        }
        return left;
    }
};



相关文章推荐

leetcode209-Minimum Size Subarray Sum(最小长度子数组和)

问题描述:Given an array of n positive integers and a positive integer s, find the minimal length of a su...
  • will130
  • will130
  • 2016年01月25日 23:15
  • 310

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, nu...

【LeetCode】209. Minimum Size Subarray Sum

题目Given an array of n positive integers and a positive integer s, find the minimal length of a subar...

Leetcode 209 Minimum Size Subarray Sum

给出一个正整数数组,以及一个正整数s,找到最短的子数组且该子数组的和不小于s。如果不存在这样的子数组,返回0....

Leetcode 209. Minimum Size Subarray Sum

Given an array of n positive integers and a positive integer s, find the minimal length of a contigu...

Leetcode——209. Minimum Size Subarray Sum

题目: Given an array of n positive integers and a positive integer s, find the minimal length of a co...

Leetcode 209 - Minimum Size Subarray Sum(二分 or 双指针)

题意给一个数组a[]和一个数s,要求在a里面找到一个长度最短的连续子序列,并且子序列的和大于等于s。思路算法1前缀和 + 二分。时间复杂度O(nlogn)O(nlogn)。我们先维护一个前缀和ss,然...
  • Lzedo
  • Lzedo
  • 2017年02月12日 20:35
  • 117

Minimum Size Subarray Sum - LeetCode 209

题目描述: Given an array of n positive integers and a positive integer s, find the minimal length of a ...
  • bu_min
  • bu_min
  • 2015年05月24日 20:52
  • 206

LeetCode 209. Minimum Size Subarray Sum 滑动窗口

一、题目 Given an array of n positive integers and a positive integer s, find the minimal length of a ...

【leetcode】209. Minimum Size Subarray Sum

【leetcode】209. Minimum Size Subarray Sum
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[leetcode] 209. Minimum Size Subarray Sum
举报原因:
原因补充:

(最多只允许输入30个字)