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. Eclipse rap 富客户端开发总结(1) :rap简单介绍和开发环境搭建

    一.rap简单介绍 1 基本概念     RAP可以让开发人员使用JAVA API和按照Eclipse 插件的开发模式构建基于AJAX的Web 2.0应用程序, RAP的工作原理是采用交叉编译的方式将 ...

  2. Maven第三篇【Maven术语、pom.xml介绍】

    maven术语 在我们上一篇中已经知道了在Intellij idea下是如何使用Maven的了,创建出来的目录结构是这样子的: 上面的目录结构就是Maven所谓的"约定",我们使用 ...

  3. webservice07#契约优先#webservice实现简单的动态web项目

    1, 用户管理 User{username,password,nickname} 属性. 2,契约优先[ 先用schema做标准来写wsdl.再生成服务器端的接口,再编写接口的类] 在src下创建目录 ...

  4. windows10 下安装node失败 出现2502 2503的解决办法

    下载node出现以下异常    : the install has encountered an unexpected errer installing this package.this may i ...

  5. AngularJS中的DOM与事件

      前 言 AngularJS中的DOM与事件   AngularJS 为 HTML DOM 元素的属性提供了绑定应用数据的指令.  ng-disabled="true/false" ...

  6. MySQL_日期函数汇总

              如果转载,请注明博文来源: www.cnblogs.com/xinysu/   ,版权归 博客园 苏家小萝卜 所有.望各位支持!         关于MySQL日期时间函数,每回总 ...

  7. 深入理解计算机系统chapter9

    从概念上来讲:虚拟存储器被组织为一个存放在磁盘上的N个连续的字节大小的单元组成的数组. 磁盘上数组的内容被缓存到主存中 1. 读写内存的安全性 物理内存本身是不限制访问的,任何地址都可以读写,而操作系 ...

  8. java集合系列——Map介绍(七)

    一.Map概述 0.前言 首先介绍Map集合,因为Set的实现类都是基于Map来实现的(如,HashSet是通过HashMap实现的,TreeSet是通过TreeMap实现的). 1:介绍 将键映射到 ...

  9. Select()使用否?

    David Treadwell ,Windows Socket 的一位开发者,曾经在他的一篇名为"Developing Transport-Independent Applications ...

  10. java一些问题的思考

    1.思考 为什么java规定作为程序入口点的main() 方法静态的? 在java中,main()方法是java应用程序的入口方法,也就是说,程序在运行的时候,第一个执行的方法就是main()方法,这 ...