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 ...
随机推荐
- MultipartEntity 乱码
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Ch ...
- dp背包之01背包poj2184
http://poj.org/problem?id=2184 题意:给定两个属性,求这两个属性的和的最大值......... 思路:将第一个属性往后平移1000个单位,然后推导其动态转移方程,若是dp ...
- DelphiXE8FMX工程实现无边框托动(发送消息)
1.引用单元 uses Winapi.Windows, FMX.Platform.Win, Winapi.Messages; 2.发送消息 //发送系统消息SendMessage(FmxHandleT ...
- 基于jQuery仿淘宝产品图片放大镜代码
今天给大家分享一款 基于jQuery淘宝产品图片放大镜代码.这是一款基于jquery.imagezoom插件实现的jQuery放大镜.适用浏览器:IE8.360.FireFox.Chrome.Safa ...
- Unix系统编程()原子操作和竞争条件
竞争状态是这样一种情形:操作共享资源的两个进程(或线程),其结果取决于一个无法预期的顺序,即这些进程获得CPU使用权的先后相对顺序. 以独占的方式创建一个文件 当同时指定了O_EXCL和O_CREAT ...
- oozie开发注意事项
ooziejob执行后 1. job.properties.coordinatior.xml中设置的值都是不可变的,除非将job kill掉,然后重新调度. oozie job -kill 00000 ...
- php if语句判定ms查询是否为空
$select_re_id=$cnx->query("select count(*) c from Yid where pid='".$pid."' and gid ...
- Servlet和JSP规范及版本对应关系
JSR 53: JavaTM Servlet 2.3 and JavaServer PagesTM 1.2 JSR 154: JavaTM Servlet 2.4 JSR 154: JavaTM Se ...
- imx6 android SD卡启动
工作中需要将imx6的android系统从SD卡启动,所以就分析了MfgTool中的脚本,分析android的分区情况,并尝试自己操作,竟然成功了,记录于此. 参考文档 http://www.kanc ...
- 【BZOJ】1679: [Usaco2005 Jan]Moo Volume 牛的呼声(数学)
http://www.lydsy.com/JudgeOnline/problem.php?id=1679 水题没啥好说的..自己用笔画画就懂了 将点排序,然后每一次的点到后边点的声音距离和==(n-i ...