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

click to show more practice.

Credits:
Special thanks to @Freezen for adding this problem and creating all test cases. 

Hide Tags
 Array Two Pointers Binary Search 

链接: http://leetcode.com/problems/minimum-size-subarray-sum/

题解:

首先想到滑动窗口sliding window,从0开始累加,当和Sum大于等于S的时候再从左侧减小窗口,继续进行判断。

Time Complexity - O(n),Space Complexity - O(1)

public class Solution {
    public int minSubArrayLen(int s, int[] nums) {      //sliding window
        if(nums == null || nums.length == 0 || s <= 0)
            return 0;
        int sum = 0, minLen = Integer.MAX_VALUE, left = 0;
        
        for(int right = 0; right < nums.length; right++){
            sum += nums[right];
            
            while(sum >= s){
                minLen = Math.min(right - left + 1, minLen);
                sum -= nums[left++];
            }
        }
        
        return minLen != Integer.MAX_VALUE ? minLen : 0;
    }
}

Update:

public class Solution {
    public int minSubArrayLen(int s, int[] nums) {
        if(nums == null || nums.length == 0 || s < 0)
            return 0;
        
        int sum = 0, minLen = Integer.MAX_VALUE, lo = 0;
        
        for(int i = 0; i < nums.length; i++) {
            sum += nums[i];
            
            while(sum >= s) {
                minLen = Math.min(minLen, i - lo + 1);
                sum -= nums[lo++];
            }
        }
        
        return minLen == Integer.MAX_VALUE ? 0 : minLen;
    }
}

二刷:

方法和一刷一样,是一道典型的sliding window题目。

我们可以先累加, sum += nums[i],当满足条件sum >= s的时候,我们尝试缩小左边界, 即用一个while循环来减少左边界。计算完毕以后尝试更新minLen。

Java:

Time Complexity - O(n),Space Complexity - O(1)

public class Solution {
    public int minSubArrayLen(int s, int[] nums) {
        if (nums == null || nums.length == 0) return 0;
        int minLen = Integer.MAX_VALUE, lo = 0, sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            if (sum >= s) {
                while (sum >= s && lo <= i) sum -= nums[lo++];
                minLen = Math.min(minLen, i - lo + 2);
            }
        }
        return minLen != Integer.MAX_VALUE ? minLen : 0;
    }
}

三刷:

Java:

public class Solution {
    public int minSubArrayLen(int s, int[] nums) {
        if (nums == null || nums.length == 0) return 0;
        int minLen = Integer.MAX_VALUE, lo = 0, sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            if (sum >= s) {
                while (sum >= s) sum -= nums[lo++];
                minLen = Math.min(minLen, i - lo + 2);
            }
        }
        return minLen != Integer.MAX_VALUE ? minLen : 0;
    }
}

Reference:

原文地址:https://www.cnblogs.com/yrbbest/p/4496653.html