leetcode面试准备:Minimum Size Subarray Sum

1 题目

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.

接口:public int minSubArrayLen(int s, int[] nums)

2 思路

题意

给定一个包含n个正整数的数组和一个正整数s,找出其满足和sum ≥ s的子数组的最小长度。如果不存在这样的子数组,返回0

例如,给定数组 [2,3,1,2,4,3]与s = 7,

子数组[4,3]具有满足题设条件的最小长度。

解题思路

O(n^2)解法:贪心法,要点:一个子数组是结果,一定有某个为起点的位置。网上说:滑动窗口法。不知道是怎么解的。

O(nlogn)解法:二分枚举,思路是,我们建立一个比原数组长一位的sums数组,其中sums[i]表示nums数组中[0, i - 1]的和,然后我们对于sums中每一个值sums[i],用二分查找法找到子数组的右边界位置,使该子数组之和大于sums[i] + s,然后我们更新最短长度的距离即可。

3 代码

    /**
* Time:O(n^2) Space:O(1) 有更好的解法,时间复杂度:O(nlogn)
*/
public int minSubArrayLen(int s, int[] nums) {
int min = Integer.MAX_VALUE;
int len = nums.length;
for (int i = 0; i < len; i++) {
int count = 0, sum = 0;
for (int j = i; j < len; j++) {
sum += nums[j];
count++;
if (sum >= s) {
min = Math.min(min, count);
break;
}
}
}
return min == Integer.MAX_VALUE ? 0 : min;
}

4 总结

二分法的解法,不是很明白。手动走一遍,二分法的代码。

leetcode面试准备: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 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 ...

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

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

  4. 【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 ...

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

  6. 【Leetcode】209. Minimum Size Subarray Sum

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

  7. [LeetCode] Minimum Size Subarray Sum 解题思路

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

  8. [LintCode] Minimum Size Subarray Sum 最小子数组和的大小

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

  9. 领扣-209 长度最小的子数组 Minimum Size Subarray Sum MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

随机推荐

  1. Nginx+Tomcat+memcached负载均衡实现session共享

    http://blog.csdn.net/love_ubuntu/article/details/8464983 1.  安装各个软件不用说了. 2.  到tomcat的安装目录lib中,加入: me ...

  2. [firefly]暗黑源码解析

    一.架构 二.各模块详解 1.net 2.gate 3.game 4.db 三.启动 四.举例说明 五.性能测试

  3. ReactNative-----环境搭建二(android)

    一.初始化一个ReactNative项目 在指定目录运行命令:react-native init Vince(项目名称)  //其过程就是在使用CLI工具构建项目, 命令行代码 F:\React> ...

  4. js window.open 参数设置

    function OpenWin(type, obj){ window.open ("http://www.baidu.com" + type, "_blank" ...

  5. 给python类动态添加方法(method)

    群里有人问如何做到 def foo(): pass class Bar(object): pass Bar.set_instance_method(foo) b = Bar() b.foo() 这个其 ...

  6. nginx 安装过程中遇到的问题

    安装Nginx时报错 ./configure: error: the HTTP rewrite module requires the PCRE library. 安装pcre-devel解决问题yu ...

  7. php加了命名空间没引入初始化文件:类的命名空间要与文件夹名一致namespace Business\Event;缺少了Event

    php加了命名空间没引入初始化文件:类的命名空间要与文件夹名一致namespace Business\Event;缺少了Event

  8. mysqli 扩展库的预处理技术(mysqli_stmt)

    提出问题 现在需要向mysql数据库中添加100个用户,请问如何实现? 方法一:for循环100次 方法二:使用批量添加 $sqls="insert xxx"; $sqls.=&q ...

  9. Linux-Tcp-IP

    /* tcpcli.c */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include ...

  10. SQL SERVER 2008 使用TDE加密和解密

    SQL SERVER 2008 加密和解密,这样的文件在互联网上不胜枚举,本文的寓意还是一样,一为记录,二可以为开发者提供在实现过程中的注意事项. TDE: Transparent data encr ...