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- ...
随机推荐
- Python中的shelve模块
shelve中有用的函数就是open(),但是下面编写的数据库函数中调用路径是经常出错,如果直接调用一个从来没有用过的文件却能正常运行,暂时没有找出原因. 调用shelve.open()会返回一个sh ...
- ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现
ASP.NET MVC 学习笔记-2.Razor语法 1. 表达式 表达式必须跟在“@”符号之后, 2. 代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...
- LeetCode题解汇总
陆续更新至github... https://github.com/OliveLv/LeetCode/
- Redis Server分布式缓存编程
这篇文章我将介绍如果用最简洁的方式配置Redis Server, 以及如何使用C#和它交互编程 一. 背景介绍 Redis是最快的key-value分布式缓存之一 缺点: 没有本地数据缓冲, 目前还没 ...
- Android Client and PHP Server
1 FEApplication https://github.com/eltld/FEApplication https://github.com/eltld/FE-web https://githu ...
- STM32的精确延时
/*---------------------------------------------------------- 文件名:systick.c 文件描写叙述:sysTick 系统滴答时钟1us中 ...
- 嵌入式开发之命令行---linux下的find文件查找命令与grep文件内容查找命令
在使用linux时,经常需要进行文件查找.其中查找的命令主要有find和grep.两个命令是有区的. 区别:(1)find命令是根据文件的属性进行查找,如文件名,文件大小,所有者,所属组,是否为空,访 ...
- 【iOS系列】-xib封装使用
[iOS系列]-xib封装使用 Xib文件可以用来描述某一块局部的UI界面 Xib文件的加载 修改xib文件的大小size(Freeform) 第一: NSArray *objs = [[NSBund ...
- 深入分析JavaWeb Item13 -- jsp指令具体解释
一.JSP指令简单介绍 JSP指令(directive)是为JSP引擎而设计的.它们并不直接产生不论什么可见输出,而仅仅是告诉引擎怎样处理JSP页面中的其余部分. 在JSP 2.0规范中共定义了三个指 ...
- 6.游戏特别离不开脚本(3)-JS脚本操作java(直接解析JS公式,并非完整JS文件或者函数)
engine.put("usList", us); engine.put("obj", new JSModifiedJava()) ; 取个变量名就put进去 ...