leetcode@ [209]Minimum Size Subarray Sum
2015-11-01 15:19
351 查看
https://leetcode.com/problems/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
the subarray
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.
class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { vector<int> vec = nums; for(int i=1;i<nums.size();++i){ nums[i]+=nums[i-1]; } int minLen = numeric_limits<int>::max(); for(int i=0;i<nums.size();++i){ int low = i, high = nums.size()-1, mid; while(low < high){ mid = (low+high)/2; if(nums[mid]-nums[i]+vec[i] < s) low = mid+1; else high = mid; } if(nums[low]-nums[i]+vec[i]>=s && low-i+1<minLen) minLen = low-i+1; } if(minLen == numeric_limits<int>::max()) return 0; else return minLen; } };
相关文章推荐
- Codestorm:Game with a Boomerang
- maven的聚合和继承(三)
- C++primer学习:模板编程(2):类模板的定义
- 证明N个节点构成一棵树的种类数
- Mysql学习笔记一, 安装Mysql,简单命令学习
- 记一次被中间人攻击的经历
- Terminal终端的快捷键
- Mysql:is not allowed to connect to this MySQL server
- Linux文件系统分析之五(常规文件的表示)
- Linux文件系统分析之四(目录项)
- 高通Adreno图形处理器全解析
- 学习code的几个相当不错的搜索引擎
- Eclipse添加和查看书签
- 原型模式
- Rust基础笔记:Getting input from the console
- 【啊哈!算法】最快最简单的排序——桶排序
- 往SDE和MDB数据库加载数据的问题
- Linux文件系统分析之三(i节点区)
- Linux文件系统分析之二(超级块,i节点位图和逻辑块位图)
- php学习之 array_merge与操作符+号的区别