[LeetCode] Minimum Size Subarray Sum 解题思路
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 解题思路的更多相关文章
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- (leetcode)Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...
- LeetCode Minimum Size Subarray Sum (最短子序列和)
题意:给一个序列,找出其中一个连续子序列,其和大于s但是所含元素最少.返回其长度.0代表整个序列之和均小于s. 思路:O(n)的方法容易想.就是扫一遍,当子序列和大于s时就一直删减子序列前面的一个元素 ...
- LeetCode—Minimum Size Subarray Sum
题目: Given an array of n positive integers and a positive integer s, find the minimal length of a sub ...
- leetcode面试准备:Minimum Size Subarray Sum
leetcode面试准备:Minimum Size Subarray Sum 1 题目 Given an array of n positive integers and a positive int ...
- 【刷题-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 ...
- [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 ...
- [LintCode] Minimum Size Subarray Sum 最小子数组和的大小
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
随机推荐
- JS将时间戳转换为JS Date类型
/*将JSON Date 格式转换为JavaScript 的Date 类型JSON Date 格式:"/Date(146471041000)/"*/function JSONDat ...
- 测试php页面执行代码时间
//生命一个计算脚本运行时间的类 class Timer { private $startTime = 0; //保存脚本开始执行时的时间(以微秒的形式保存) private $stopTime = ...
- 在 Linux 命令行中使用和执行 PHP 代码
PHP是一个开源服务器端脚本语言,最初这三个字母代表的是“Personal Home Page”,而现在则代表的是“PHP:Hypertext Preprocessor”,它是个递归首字母缩写.它是一 ...
- 水晶報表中小寫變大寫的函數-VB
Function total (ls as number) as string dim dx_sz as string dim dx_dw as string dim str_int as strin ...
- gulp解决RequireJS
gulp解决RequireJS项目前端缓存问题(二) 前言 这一节,我们主要解决在上一节<使用gulp解决RequireJSs项目前端缓存问题(一)>末尾提到的几个问题: 对通过req ...
- BZOJ 1707: [Usaco2007 Nov]tanning分配防晒霜
Description 奶牛们计划着去海滩上享受日光浴.为了避免皮肤被阳光灼伤,所有C(1 <= C <= 2500)头奶牛必须在出门之前在身上抹防晒霜.第i头奶牛适合的最小和最 大的SP ...
- 3G? 2G? 2.5G? 4G? 与 WIFI, GPRS,CDMA 3G无线上网
首先说说无线上网有哪几种形式? WIFI, GPRS, CDMA 3G无线上网 1>wifi全称wireless fidelity,是当今使用最广的一种无线网络传输技术.实际上就是把有线网络信号 ...
- ThreadLocal学习
1.简介: 类ThreadLocal<T>,为变量提供了线程本地化副本.对于用ThreadLocal维护的变量,当前线程中的副本不同于它在其他线程中的副本,每个线程通过ThreadLoca ...
- VLAN间单臂路由访问
实验书上的拓朴图: 注意TRUNK端口和路由器子端口设置,可以承载不同的VLAN标签. 交换机(用2691加交换模块实现的): Building configuration... Current co ...
- MyEclipse10.6导出war包出错
在右键选中项目->export->java ee ->war 的时候,一点就报错SECURITY ALERT:INTEGRITY CHECK ...,之后自动关闭 这个问题是因为用的 ...