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.

问题:给定一个正整数数组和一个正整数 s ,求连续子数组的和大于等于 s 的最小长度。

解题思路:采用滑动窗口算法(Slide Window Algorithm)。

设下标 l 和 r, 把左开右闭 [l, r) 想象成一个窗口。

  • 当窗口内的和 sum 小于 s 时, 则 r 向右滑动,增加窗口区间。
  • 当窗口内的和 sum 大于等于 s 时,表示已经满足原题目要求,是一个可行解,解 l 向右滑动,继续求解。
int minSubArrayLen(int s, vector<int>& nums) {

    int l = ;
int r = ; int sum = ;
int res = intMax; while (r < nums.size()) {
sum += nums[r];
r++;
while (sum >= s) {
res = min(res, r-l);
sum = sum - nums[l];
l++;
}
} return (res == intMax) ? : res;
}

额外记录:

Slide Window Algorithm 可能不是一个正式的称谓,因为在 wikipedia 没有找到介绍。

遇到求最小 / 最大连续子数组,好像都可以考虑使用 Slide Window Algorithm 。

参考资料:

LeetCode Minimum Size Subarray Sum, jyuan

[LeetCode] Minimum Size Subarray Sum 解题思路的更多相关文章

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

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

  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. 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)

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

  4. LeetCode Minimum Size Subarray Sum (最短子序列和)

    题意:给一个序列,找出其中一个连续子序列,其和大于s但是所含元素最少.返回其长度.0代表整个序列之和均小于s. 思路:O(n)的方法容易想.就是扫一遍,当子序列和大于s时就一直删减子序列前面的一个元素 ...

  5. LeetCode—Minimum Size Subarray Sum

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

  6. leetcode面试准备:Minimum Size Subarray Sum

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

  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] Maximum Size Subarray Sum Equals k 最大子数组之和为k

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

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

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

随机推荐

  1. 用javascript操作xml(二)JavaScript 将XML转换成字符串(xml to string)

    function xmlToString(xmlData) { var xmlString; //IE if (window.ActiveXObject){ xmlString = xmlData.x ...

  2. 翻译:ECMAScript 5.1简介

    简介 ECMAScript 5.1 (或仅 ES5) 是ECMAScript(基于JavaScript的规范)标准最新修正. 与HTML5规范进程本质类似,ES5通过对现有JavaScript方法添加 ...

  3. C语言中输入输出格式控制

    1.C语言中,非零值为真,真用1表示:零值为假,假用0表示. 2.转义字符参考: \a 蜂鸣,响铃 \b 回退:向后退一格 \f 换页 \n 换行 \r 回车,光标到本行行首 \t 水平制表 \v 垂 ...

  4. What is Windows Clustering

    A cluster is a group of independent computer systems, referred to as nodes, working together as a un ...

  5. osg学习笔记2, 命令行参数解析器ArgumentParser

    ArgumentParser主要负责命令行参数的读取 #include <osgDB/ReadFile> #include <osgViewer/Viewer> int mai ...

  6. Visual C++ 6.0常用快捷键

    一.常用编译相关的快捷键 1.编译(单个文件)  Ctrl+F7 2.连接 F7 3.运行  Ctrl+F5 二.常用调试相关的快捷键 1.GO(全速运行)  F5 2.Stop Debuging(停 ...

  7. Github readme语法-- markdown

    README 该文件用来测试和展示书写README的各种markdown语法.GitHub的markdown语法在标准的markdown语法基础上做了扩充,称之为GitHub Flavored Mar ...

  8. 把 Eclipse 中的工程 Push 到 Github(适用 Windows 平台)

    今天发现一小技巧,关于如何把Eclipse的某一个Existing project push 到github服务器. Eclipse 应该是 JavaEE 版本. 在project 右键 team, ...

  9. tornado异步请求的理解(转)

    tornado异步请求的理解 http://www.kankanews.com/ICkengine/archives/88953.shtml 官网第一段话: Tornado is a Python w ...

  10. 李洪强漫谈iOS开发[C语言-031]-逻辑短路