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.

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


题目标签:Array, Two Pointers, Binary Search

  题目给了一个数组,和一个 s, 让我们找到一个 最短的连续子数组,它的数组之和大于等于 s。

  题目说了两种方法:O(n) 和 O(n log n)

  方法1: O(n) 利用 Two Pointers

  设一个start 和一个 end,用sliding window, 一个窗口,start 到 end。

  当 这个窗口的sum 小于 s 的时候,说明数字还不够大,需要更多数字,就延伸右边的 end 来得到更多数字;

  当 这个窗口的sum 大于等于 s 的时候,说明 我们得到了一个 可能是答案的 window size,我们需要最小的。 然后把左边的start 向右延伸,继续寻找新的可能性。

  

Java Solution:

Runtime beats 24.17%

完成日期:09/04/2017

关键词:Array,Two Pointers

关键点:Sliding window 控制左右边界,来找到最小的符合要求的 window size

 class Solution
{
public int minSubArrayLen(int s, int[] nums)
{
/* Solution 1: O(n)*/
if(nums.length == 0)
return 0; int start = 0;
int end = 0;
int res = Integer.MAX_VALUE;
int sum = nums[start]; while(true)
{
if(sum < s)
{
// shift end to right one place
if(end + 1 == nums.length)
break; sum += nums[++end];
}
else if(sum >= s)
{
// get the smaller size
res = Math.min(res, end - start + 1); if(end == start)
break; // shift start to right one place
sum -= nums[start++];
}
} if(res == Integer.MAX_VALUE)
res = 0; return res;
}
}

  方法2:O(n log n) 利用 二分法来找到最小符合要求的 window size

  要利用 binary search 的话,需要一个有序的数组,所以我们需要新设一个 原nums size + 1 的新 array sums, 把每一个从0 到 i (在nums中) 的sum 存入 新 array 的 index 1 到最后。

  所以新的array 里, 从index 1 到最后的 每一个值 都是 原nums 相对应的 0 到 i 的sum。

  原题例子:

  nums 2 3 1 2 4 3    原 array

  sums 0 2 5 6 8 12 15  新 array 第一个位置不用,后面每一个数字都是 nums 里相对应的sum值

  在有了这个有序数组之后, 可以遍历新 array index 1 开始的每一个数字, 找到每一个最小的右边边界 (sums[i] + s) ,然后记录一个最小的window size 就可以了。

Java Solution:

Runtime beats 12.29%

完成日期:09/04/2017

关键词:Array,Binary Search

关键点:新建一个 ascending sums array 对应 nums array;

    用二分法找到符合s要求的最小的window size

 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;
} public 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;
}
}

参考资料:

https://discuss.leetcode.com/topic/13749/two-ac-solutions-in-java-with-time-complexity-of-n-and-nlogn-with-explanation

  

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 209. Minimum Size Subarray Sum (最短子数组之和)的更多相关文章

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

  2. [LeetCode] Minimum Size Subarray Sum 最短子数组之和

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

  3. Minimum Size Subarray Sum 最短子数组之和

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

  4. LeetCode 209 Minimum Size Subarray Sum

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

  5. Java for LeetCode 209 Minimum Size Subarray Sum

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

  6. 【刷题-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 ...

  7. LeetCode OJ 209. Minimum Size Subarray Sum

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

  8. [LeetCode] 930. Binary Subarrays With Sum 二元子数组之和

    In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0, ...

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

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

随机推荐

  1. Java项目生成Jar文件

    打开 Jar 文件向导 Jar 文件向导可用于将项目导出为可运行的 jar 包. 打开向导的步骤为: 在 Package Explorer 中选择你要导出的项目内容.如果你要导出项目中所有的类和资源, ...

  2. 利用Vim提供的正则去掉代码每行开头不想要的行号以及vi常见问题和应用技巧

    看到一不错的代码片断,但奈何无论怎么拷贝,粘贴到VS里都是带行号的,300多行的代码手工删除行号与前面的空格可得耗不少时间...这时想起了无所不能的VIM,以及它的正则,它的替换功能.解决步骤: 1. ...

  3. 《Java从入门到放弃》JavaSE入门篇:文件操作

    Java中的文件操作还有点小复杂··· 不过没关系,我会把它讲得很简单,嘿嘿嘿!!! 在讲Java中的文件操作前,先了解一个概念--"流",比如我们把一个杯子的水倒到另一个同样大小 ...

  4. Spring - bean的autowire属性(自动装配)

    当我们要往一个bean的某个属性里注入另外一个bean,我们会使用<property> + <ref/>标签的形式.但是对于大型项目,假设有一个bean A被多个bean引用注 ...

  5. 来自projecteuler.net网站的练习题2

    0.题目如下: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By st ...

  6. 入坑IT都快十年了

    一起帮的开发直播已经告一段落:一是主体的功能差不多都实现了,二是用到的架构技术都展示得差不多了.以后就算继续开发,也应该都是一些“技术上”重复的工作而已.整个直播过程耗时近半年,SVN提交1062次, ...

  7. 我是如何利用Hadoop做大规模日志压缩的

    背景 刚毕业那几年有幸进入了当时非常热门的某社交网站,在数据平台部从事大数据开发相关的工作.从日志收集.存储.数据仓库建设.数据统计.数据展示都接触了一遍,比较早的赶上了大数据热这波浪潮.虽然今天的人 ...

  8. hdu 1542 线段树 求矩形并

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  9. (转)simhash算法原理及实现

    simhash是google用来处理海量文本去重的算法. google出品,你懂的. simhash最牛逼的一点就是将一个文档,最后转换成一个64位的字节,暂且称之为特征字,然后判断重复只需要判断他们 ...

  10. VB.NET生成重复窗体

    Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ...