zoukankan      html  css  js  c++  java
  • 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:

  • 相关阅读:
    洛谷 P2234 [HNOI2002]营业额统计
    洛谷p3146&p3147
    洛谷 p1439 最长公共子序列
    搜索
    一步一步分析Caliburn.Micro(二:绑定执行方法Message现学现卖之自定命令)
    一步一步分析Caliburn.Micro(一:绑定执行方法Message)
    整理的C# 字符串类
    不用ADOX.CatalogClass创建Access数据库文件
    取远程网页数据 WebClient,HttpWebRequest
    C# LinQ 与 ADO.NET
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4496653.html
Copyright © 2011-2022 走看看