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

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


【题目分析】

给定一个由n个正数组成的数组一个目标值s,找到大于等于目标值的最短子数组的长度,如果不存在就返回0。


【思路】

1. 时间复杂度为O(n)

为了找到最小的子数组,并且子数组元素的和值要大于等于s,我们要找到子数组的起始点和终止节点。假设我们已经找到了一个满足条件的子数组,下一步该怎么办呢?

为了使得数组的长度最短,我们要把起始点向后移动,如果还满足条件就继续向后移动。如果条件不满足了,我们就把终止节点向后移动,然后重复上面的过程。举个例子如下:nums: [2,3,1,2,4,3],s=7。

(1)begin=0, end=0, sum=2,minlen=nums.length+1; 此时sum<7,end向后移动。

(2)sum=5,end继续向后移动。

(3)sum=6,end继续向后移动。

(4)sum=8,begin向后移动,minlen=4。

(5)sum=6,end向后移动。

(6)sum=9,begin向后移动,minlen=4。

(7)sum=7,begin继续向后移动,minlen=3。

(8)sum=6,end向后移动。

(9)sum=9,begin向后移动,minlen=3.

(10)sum=7,begin向后移动,minlen=2.

(11)结束,返回2.

2. 时间复杂度为nO(logn)

As to NLogN solution, logN immediately reminds you of binary search. In this case, you cannot sort as the current order actually matters. How does one get an ordered array then? Since all elements are positive, the cumulative sum must be strictly increasing. Then, a subarray sum can expressed as the difference between two cumulative sum. Hence, given a start index for the cumulative sum array, the other end index can be searched using binary search.

由于数组中所有元素是正数,因此向后累加的和值肯定是递增的。一个子数组的和可以表示为两个不同位置数组累加和的差值。所以我们构造一个数组累加和的数组,然后使用二分查找来解决这个问题。

例如 nums:[2,3,1,2,4,3],s=7。累加和数组sums:[0,2,5,6,8,12,15].

我们遍历sums数组,对于每一个元素sum[i],向后找到第一个比sums[i]+s大的元素,然后计算此时的minlen的长度。返回最小的即可。


【java代码1】O(N)

 public class Solution {
public int minSubArrayLen(int s, int[] nums) {
if(nums == null || nums.length == 0) return 0; int begin = 0, end = 0;
int sum = nums[0], minlen = nums.length + 1; while(end < nums.length){
if(sum < s){
if(end < nums.length-1)
sum += nums[++end];
else break;
}
else{
minlen = Math.min(minlen, end-begin+1);
sum = sum - nums[begin++];
}
} return minlen <= nums.length ? minlen : 0;
}
}

【java代码1】NO(logN)

 public class Solution {
public int minSubArrayLen(int s, int[] nums) {
int[] sums = new int[nums.length + 1];
for (int i = 1; i < sums.length; i++) sums[i] = sums[i - 1] + nums[i - 1];
int minLen = Integer.MAX_VALUE;
for (int i = 0; i < sums.length; i++) {
int end = binarySearch(i + 1, sums.length - 1, sums[i] + s, sums);
if (end == sums.length) break;
if (end - i < minLen) minLen = end - i;
}
return minLen == Integer.MAX_VALUE ? 0 : minLen;
} private int binarySearch(int lo, int hi, int key, int[] sums) {
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (sums[mid] >= key){
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return lo;
}
}

LeetCode OJ 209. Minimum Size Subarray Sum的更多相关文章

  1. 【刷题-LeetCode】209. Minimum Size Subarray Sum

    Minimum Size Subarray Sum Given an array of n positive integers and a positive integer s, find the m ...

  2. 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...

  3. LeetCode OJ:Minimum Size Subarray Sum(最小子数组的和)

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

  4. 【Leetcode】209. Minimum Size Subarray Sum

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

  5. leetcode面试准备:Minimum Size Subarray Sum

    leetcode面试准备:Minimum Size Subarray Sum 1 题目 Given an array of n positive integers and a positive int ...

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

  7. 209. Minimum Size Subarray Sum(双指针)

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

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

  9. LeetCode 209 Minimum Size Subarray Sum

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

随机推荐

  1. 在python上获得随机字符

    """今天写一个程序,在想既然可以获得随机数,那我可不可以获得任意字符呢,于是在stackoverflow.com 上找到了方法,几乎都是用导入random,然后再用其它 ...

  2. flume 以 kafka 为channel 的配置

    #此配置以kafka的一个topic为channel,相比其他channel类型 file和cache 兼并了快和安全的要求!# Define a kafka channel a1.channels. ...

  3. ele.me在IOS浏览器端启动APP的技巧分析

    ele.me在IOS浏览器端启动APP的技巧分析 巧妙利用后台重定向,在schemes启动时提示用户打开,启动不了APP时能够及时跳转至下载页面. 避免报错页面的出现以及用户还没来的及选择就跳转到下载 ...

  4. JTree单击事件

    import javax.swing.*; import javax.swing.tree.*; import java.awt.FlowLayout; import java.awt.GridLay ...

  5. ajax中的stasus错误详解

    一.英文版解析 0: (Uninitialized) the send( ) method has not yet been invoked. 1: (Loading) the send( ) met ...

  6. 【USACO】草地排水

    Drainage Ditches 草地排水 usaco 4.2.1描述在农夫约翰的农场上,每逢下雨,Bessie最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一 ...

  7. tomcat服务配置及搭建

    一.在官网上下载tomcat 下载地址:http://tomcat.apache.org/download-60.cgi 下载完后解压 二.设置环境变量 1,JAVA_HOME 2.CATALINA_ ...

  8. Framebuffer的配置及应用——先转载留着,以后一定要弄懂

    http://blog.csdn.net/tju355/article/details/6881389   借助于framebuffer,我们能够在console下面作很多事情.首先下载framebu ...

  9. tomcat + jenkins启动tomcat后打开jenkins页面提示404错误的解决方案

    首先下载tomcat和jenkins,将下载的jenkins2.12 war放到tomcat的webapps文件夹下后执行/bin文件夹下的start启动后,打开http://localhost:80 ...

  10. svn中“clean up”死循环问题解决办法

    SVN在使用update命令时,提示使用"clean up "命令,在使用clean up命令时报错"Previous operation has not finishe ...