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. windows phone 8 设置锁屏背景

    本来想研究一下 利用闪光灯实现手电筒的代码,发现不是简答设置FlashMode属性可以解决问题的,ms也没有提供api,无意瞄了一眼侧边栏的文章列表,发现了设置屏幕锁屏背景的实现,手一抖点进去了.还算 ...

  2. javascript 节点的增,删,改,查

    1.创建节点  A.创建元素节点    document.createElement("元素标签名");   B.创建属性节点    document.createAttribut ...

  3. DevExpress 控件使用之BarManager

    DevExpress 开发的控件有很强的实力,不仅功能丰富,应用简便,而且界面华丽,更可方便定制.对于编程人员来说是个不错的选择.它的菜单栏控件更具代表,完全可以替代开发环境提供的基本控件,而让您编写 ...

  4. linux重新增加硬盘容量

    1.先用df -h查看硬盘使用情况 2.fdisk -l查看分区情况 表示还没有挂载 3.fdisk /dev/vdb进行分区 4.mkfs.ext3 /dev/vdb进行格式化 5.mount /d ...

  5. Python的简介以及安装和第一个程序以及用法

    Python的简介: 1.Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.自从20世纪90年代初Python语言诞生至今,它逐渐被广泛应用于处理系统管理任务和Web编程.Pytho ...

  6. Swift与Objective-C的兼容“黑魔法”:@objc和Dynamic

    Cocoa框架早已烙上了不可磨灭的OC印记,而无数的第三方库都是用OC写成的,这些积累无论是谁都不能小觑.苹果采取了允许开发者在同一个项目中同时使用Swift和OC进行开发的做法,但要想实现互通,又需 ...

  7. 头文件的宏定义#ifndef测试

    一.入题 在头文件的书写中,都加入了如下内容: #ifndef __头文件名_H #define __头文件名_H #endif 曾经在书中看到的解释是“防止重复定义”,今天突然想到为什么是这样的解释 ...

  8. java 资料收集

    java中线程队列BlockingQueue的用法 为什么jdk中把String类设计成final? 深入浅出单实例Singleton设计模式

  9. 标签form表单里的属性简单介绍了一下

    <form id="form1" name="form1" method="post" action=""> ...

  10. ImageButton和Button区别

    一.基础准备       Imagebutton 继承 Imageview,就是用一个图标代表了一些文字,它没Android:text属性.它由Android:src指定图标的位置 android:s ...