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. 微软AJAX解决方案

    -------- 微软AJAX解决方案 (*) --------ASP.Net中内置的简化AJAX开发的控件UpdatePanel 放入ScriptManager,将要实现AJAX效果的控件放到Upd ...

  2. 【转】关于oracle with as用法

    原文链接:关于oracle with as用法 with as语法–针对一个别名with tmp as (select * from tb_name) –针对多个别名with   tmp as (se ...

  3. yum安装lamp环境

    装了好些次lamp环境了,都没好好总结下,现在总结下 ^ ^ 1.替换163的yum源 1.检查系统版本 cat /etc/redhat-releas   (我的版本是CentOS release 6 ...

  4. Android定时器实现方法[转]

    秒,单位毫秒Message message=new Message();message.what=1;handler.sendMessage(message);//发送消息} catch (Inter ...

  5. SCOI2015题解 && 考试小结

    Day1: 第一题:裸地二分+网络流:二分答案,连接将每行每列拆成点,对于满足答案的格子行列连边,看是否流量是否大于t即可,可惜第k大看成了第k小,然后100分就没了. 第二题:倍增,考虑贪心算法,就 ...

  6. BZOJ 1660: [Usaco2006 Nov]Bad Hair Day 乱发节

    Description Input * Line 1: 牛的数量 N. * Lines 2..N+1: 第 i+1 是一个整数,表示第i头牛的高度. Output * Line 1: 一个整数表示c[ ...

  7. 对SVM的个人理解

    对SVM的个人理解 之前以为SVM很强大很神秘,自己了解了之后发现原理并不难,不过,“大师的功力在于将idea使用数学定义它,使用物理描述它”,这一点在看SVM的数学部分的时候已经深刻的体会到了,最小 ...

  8. POJ_1220_Nmber Sequence

    上网查了一下进制转换的算法,发现一个性能比较好的:m进制转换成n进制,先用例如62进制ABC转换成10进制,就是用余位c(第一个数余位数值为0)乘以原基数from,加上A表示的数值,然后得到一个数,对 ...

  9. 控制台程序使用MFC类的方法

    (unresolved external symbol __endthreadex解决办法) 1.新建控制台程序: 2.添加源代码如下: #include <afx.h> #include ...

  10. 好看的UI设计网站 www.ui.cn 和 插画网站 www.pixiv.net 千图网,界面很不错~

    http://www.ui.cn/?t=share#project http://www.pixiv.net/ http://www.flaticon.com/ www.58pic.com 那张 给人 ...