209 Minimum Size Subarray Sum 大于给定和最短子数组
给定一个含有 n 个正整数的数组和一个正整数 s , 找到一个最小的连续子数组的长度,使得这个子数组的数字和 ≥ s 。如果不存在符合条件的子数组,返回 0。
举个例子,给定数组 [2,3,1,2,4,3] 和 s = 7,
子数组 [4,3]为符合问题要求的最小长度。
更多练习:
如果你找到了O(n) 解法, 请尝试另一个时间复杂度为O(n log n)的解法。
详见:https://leetcode.com/problems/minimum-size-subarray-sum/description/
Java实现:
方法一:时间复杂度O(nlogn)
class Solution {
public int minSubArrayLen(int s, int[] nums){
int[] sums = new int[nums.length + 1];
for (int i = 1; i < sums.length; i++){
sums[i] = sums[i - 1] + nums[i - 1];
}
int minLen = Integer.MAX_VALUE;
for (int i = 0; i < sums.length; i++){
int end = binarySearch(i + 1, sums.length - 1, sums[i] + s, sums);
if (end == sums.length){
break;
}
if (end - i < minLen) {
minLen = end - i;
}
}
return minLen == Integer.MAX_VALUE ? 0 : minLen;
}
public int binarySearch(int l, int h, int key, int[] sums){
while (l <= h){
int m = (l + h)>>1;
if (sums[m] >= key)
h = m - 1;
else
l = m + 1;
}
return l;
}
}
参考:https://www.cnblogs.com/jimmycheng/p/7477562.html
方法二:时间复杂度O(n)
class Solution {
public int minSubArrayLen(int s, int[] nums) {
int n=nums.length;
if(n==0||nums==null){
return 0;
}
int left=0;
int right=0;
int sum=0;
int res=Integer.MAX_VALUE;
while(right<n){
while(sum<s&&right<n){
sum+=nums[right++];
}
while(sum>=s){
res=Math.min(res,right-left);
sum-=nums[left++];
}
}
return res>n?0:res;
}
}
C++实现:
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int n=nums.size();
if(n==0||nums.empty())
{
return 0;
}
int left=0,right=0,sum=0,res=INT_MAX;
while(right<n)
{
while(sum<s&&right<n)
{
sum+=nums[right++];
}
while(sum>=s)
{
res=min(res,right-left);
sum-=nums[left++];
}
}
return res>n?0:res;
}
};
参考:https://www.cnblogs.com/grandyang/p/4501934.html
209 Minimum Size Subarray Sum 大于给定和最短子数组的更多相关文章
- 【刷题-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] 209. Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- LeetCode OJ 209. 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 (最短子数组之和)
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- 【Leetcode】209. Minimum Size Subarray Sum
Question: Given an array of n positive integers and a positive integer s, find the minimal length of ...
- 209. Minimum Size Subarray Sum(双指针)
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- **209. Minimum Size Subarray Sum 长度最小的子数组
1. 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 0. 示例: 输入: s = 7, nu ...
- [刷题] 209 Minimum Size Subarray Sum
要求 给定一个含有 n 个正整数的数组和一个正整数 s 找出该数组中满足其和 ≥ s 的长度最小的连续子数组 如果不存在符合条件的连续子数组,返回 0 示例 输入:s = 7, nums = [2,3 ...
- 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...
随机推荐
- Codeforces Round #221 (Div. 2) D
有点郁闷的题目,给了2000ms,可是n,m的范围已经是5000了.5000 * 5000一般在别的OJ已经是超了2000ms,一開始不敢敲.看了下别人有n*m的潜逃循环,原来CF的机子如此的强大,一 ...
- leetcode笔记:Majority Element
一. 题目描写叙述 Given an array of size n, find the majority element. The majority element is the element t ...
- 本人会linux系统的各种版本的安装,近期发教程
小弟虽然刚刚踏入职场,可是咱大学也不是打酱油过的啊,研究过各种版本系统的安装,也都均已经实践,勿喷,有问题 咱们可以相互探讨!
- 一个JS引发的跨域问题
忽然遇上跨域错误. 我们有张页面,使用了EXT.js,在本地运行正常,部署到服务器上,出不来数据.F12调试,提示有跨域错误? XMLHttpRequest cannot load http://19 ...
- Error: Target id 'android-5' is not valid. Use 'android list targets' to get the target ids.
输入命令: lianxumacdeMac-mini-2:hello-jni lianxumac$ android list targets Available Android targets: --- ...
- Where Are You Standing?
/*********************************************************************** * Where Are You Standing? * ...
- JAVA JVM 流程一
JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的.Java虚拟机 ...
- Lightoj 1140(数位DP)
求一个区间内的数含有多少个0. dp[len][pre]表示长度为len的数,含有pre个0. 需要加一个标记,来表示前缀是否为0(可以是一串连续的0),如果前缀一直为0,就一直搜,如果前缀不为0,就 ...
- 使用merge-using语句初始化数据库
创建三张表Student.Course.Enrollment CREATE TABLE [dbo].[Student] ( [StudentID] INT IDENTITY (1, 1) NOT NU ...
- jquery cloudzoom 3.0,magiczoom 放大镜插件 破解 移除版权信息
jquery Cloud Zoom一款放大镜插件.但是无奈 官方下载的始终有版权信息,因此想到如下方法去掉版权信息,测试可行! 官方网址:http://www.starplugins.com/clou ...