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. WebWork2和Spring MVC Framework的比较

    http://daihaixiang.blog.163.com/blog/static/3830134200711411515336/ WebWork2和Spring MVC Framework的比较 ...

  2. 并行计算vs分布式计算

    一般认为,集中在同一个机柜内或同一个地点的紧密耦合多处理机系统或大规模并行处理系统是并行处理系统,而用局域网或广域网连接的计算机系统是分布式处理系统.松散耦合并行计算机中的并行操作系统有时也称为分布式 ...

  3. Jquery Table添加行、删除行

    html页面代码 <table id="tblUserInfo"> </table> Js代码 function DealUserInfo(qty){ ) ...

  4. AndroidListview 滑动过程中图片显示重复错乱解决方案

    主要分析Android中Listview滚动过程造成的图片显示重复.错乱.闪烁的原因及解决方法,顺便跟进Listview的缓存机制. 1.原因分析 Listview item 缓存机制:为了使得性能更 ...

  5. Android开发之Adapter

    学习android时,对于我这种初学者来说,刚开始接触控件,发现有的控件需要adapter有些不需要,对此我感到不解.所以决定一探究竟. 其实android是一个完全遵从MVC模式的框架,activi ...

  6. Canvas实现曲线运动

    前言 Html5添加的最受欢迎的功能就是<canvas>元素,它负责在页面中设定一个区域,然后在里面可以通过javascript动态地在其内绘制图形. 主流浏览器,IE9+,手机端都是支持 ...

  7. Jstl标签库/Filter过滤器

    JSTLJSP Standard Tag Library JSP标准标签库 是Sun公司定义的一套标准,由Apache组织基于这套标准开发的一套标准库之后又转给Sun公司被称为JSTL,成为了java ...

  8. job还是job

    declare jobno binary_integer;rm_days number;rm_hour number;  --传入的hourmy_hour number;    --取出当前时间的ho ...

  9. iOS 9 Spotlight搜索 OC版

    介绍:    在WWDC 2015会议上,苹果官方公布了iOS9.除开许多新的特性和增强功能,这次升级也给了开发者们一个机会让他们的app里的内容能通过Spotlight 搜索功能被发现和使用.在iO ...

  10. [转] jQuery按键响应事件keypress对应的按键编码keycode

    原文地址:http://blog.csdn.net/chenhj1988918/article/details/7534922 keypress  api 文档:http://api.jquery.c ...