题目:

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

思路:

两个指针, start end, end向后走,直到 sum 大于 s. 然后start向后, 直到sum 小于s. 同时更新 min值。

/**
* @param {number} s
* @param {number[]} nums
* @return {number}
*/
var minSubArrayLen = function(s, nums) {
var l=0,r=0,min=2147483647,sum=0;
while(r<nums.length&&l<=r){
while(r<nums.length&&sum<s){
sum+=nums[r++];
}
while(l<=r&&sum>=s){
min=Math.min(min,r-l);
sum-=nums[l++]; }
} return min==2147483647?0:min;
};

【数组】Minimum Size Subarray Sum的更多相关文章

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

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

  2. 和大于S的最小子数组 · Minimum Size Subarray Sum

    [抄题]: 给定一个由 n 个正整数组成的数组和一个正整数 s ,请找出该数组中满足其和 ≥ s 的最小长度子数组.如果无解,则返回 -1. 给定数组 [2,3,1,2,4,3] 和 s = 7, 子 ...

  3. LeetCode 209:最小长度的子数组 Minimum Size Subarray Sum

    公众号: 爱写bug(ID:icodebugs) 作者:爱写bug 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子 ...

  4. [LintCode] 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面试准备:Minimum Size Subarray Sum

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

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

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

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

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

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

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

  10. Minimum Size Subarray Sum LT209

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

随机推荐

  1. obj-c的优缺点

    优点: 1) Cateogies : 类别 2) Posing : 扮演 3) 动态识别 : 编译时与运行时动态识别类型 4) 指标计算 : 指针计算 指针的 +- * / 5) 弹性信息传递 : 某 ...

  2. oracl中的大数据类型clob

    建表 create table test_name( test_id   number(6) not null, img_data clob ); 在java中该表所对应的po为: class Tes ...

  3. 20155339 2016-2017-2 《Java程序设计》第9周学习总结

    20155339 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 JDBC入门 JDBC简介 JDBC全名Java DataBase Connectivity ...

  4. hdu 5037 周期优化

    http://acm.hdu.edu.cn/showproblem.php?pid=5037 有只青蛙踩石子过河,河宽m,有n个石子坐标已知.青蛙每次最多跳L.现在可以在河中再放一些石子,使得青蛙过河 ...

  5. linux系统编程之进程(七):system()函数使用

    一,system()理解 功能:system()函数调用"/bin/sh -c command"执行特定的命令,阻塞当前进程直到command命令执行完毕 原型: int syst ...

  6. 【原创】Self-Contained Container(自包含容器)

    自包含容器是一种自包含的结构,很有趣.需要使用模板类,但只有在模板类型是该类的子类时,才使该类具有自包含的结构.这种结构从数据结构的角度看比较有用.通常的树类中的模板通常是“数据”的概念,即模板不参与 ...

  7. oracle 导出导入命令

      imp YG_XSOA_NEW/kingo@20.14.12.14/XSSJZX file=d:\daochu.dmp full=y (导入)   exp YG_XSOA_NEW/kingo@20 ...

  8. IIS发布网站之后,页面图片和js未加载出错

    [IIS相关]mvc做的web发布之后,运行之后界面上的图片和js都没有加载出来.      解决方案:安装IIS的时候需要勾选ASP选项,否则会出现这种情况. 如果项目是用.Netframework ...

  9. Ruby on Rails 生成指定版本的 Rails 项目

    ruby-on-rails ruby 本地 Rails 默认5.1.6 版本 $ gem list --local rails (5.1.6, 5.1.5, 5.1.4) 使用 version 生成指 ...

  10. 编程哲学之C#篇:02——学习思维

    <代码大全>的第二章:介绍隐喻(类比)的思维方式, <经济学原理>的第二章:介绍怎么像经济学家一样思考, <计算机的心智操作系统之哲学原理>的第一章:介绍学习操作系 ...