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.
代码1:遍历,复杂度O(n^2)
int min(int x,int y)
{
return x<y?x:y;
}
int minSubArrayLen(int s, int* nums, int numsSize)
{
int minlen = 100000;
int count = 0;
int i;
int j;
for(i = 0;i < numsSize;i ++)
{
count+=nums[i];
}
if(count<s)
return 0;
count = 0;
for(i = 0;i<numsSize;i++)
{
count = 0;
for(j = i;j>=0;j--)
{
count += nums[j];
if(s <= count)
{
minlen = min(minlen,i-j+1);
break;
}
}
}
return minlen;
}
C代码2:滑动窗口,复杂度O(n)
int minSubArrayLen(int s, int* nums, int numsSize)
{
int minlen = 100000;
int left = 0;
int right = 0;
int sum = 0;
while (right <= numsSize)
{
if (sum >= s)
sum -= nums[left++];
else
sum += nums[right++];
if (right - left < minlen && sum >= s)
minlen = right - left;
if(right == numsSize && sum < s)
break;
}
return minlen == 100000 ? 0 : minlen;
}
复杂度为O(n*logn)的没想出来,thinking。。。
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)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 (最短子序列和)
题意:给一个序列,找出其中一个连续子序列,其和大于s但是所含元素最少.返回其长度.0代表整个序列之和均小于s. 思路:O(n)的方法容易想.就是扫一遍,当子序列和大于s时就一直删减子序列前面的一个元素 ...
- 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 ...
- 领扣-209 长度最小的子数组 Minimum Size Subarray Sum MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
随机推荐
- httpclient 4种关闭连接
因此这样的配置就会导致每个链接至少要过180S才会被释放,这样在大量请求访问时就必然会造成链接被占满,请求等待的情况. 在通过DEBUH后发现HttpClient在method.releaseConn ...
- (转载)C++STL中vector容器的用法
vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说vec ...
- Javascript中暂停功能的实现
<script language="javascript"> /*Javascript中暂停功能的实现 Javascript本身没有暂停功能(sleep不能使用)同时 ...
- MapReduce总体架构分析
转自:http://blog.csdn.net/Androidlushangderen/article/details/41051027 继前段时间分析Redis源码一段时间之后,我即将开始接下来的一 ...
- create the web service by yourshelf
start cmd node demo.js var http = require('http'); http.createServer(function (request, response) { ...
- 001jsp的基本知识-包括生命周期,怎么编译等等
4 Jsp基础 4.1 Jsp引入 Servlet的作用: 用java语言开发动态资源的技术!!! Jsp的作用:用java语言(+html语言)开发动态资源的技术!!! Jsp就是servlet!! ...
- 【BZOJ】1176: [Balkan2007]Mokia(cdq分治)
http://www.lydsy.com/JudgeOnline/problem.php?id=1176 在写这题的时候思维非常逗啊........2333................... 最后 ...
- 【ML】有偏样本解决方案
占个位置,得空写文章. From:learning-from-imbalanced-data
- Qt slot中获取sender
调用sender();函数 例如获取一个QRadioButton QRadioButton *rb = qobject_cast<QRadioButton *>(sender());
- 83、android的消息处理机制(图+源码分析)——Looper,Handler,Message
转载:http://www.cnblogs.com/codingmyworld/archive/2011/09/12/2174255.html https://my.oschina.net/u/139 ...