当前位置:网站首页>209. Minimum length subarray - sliding window
209. Minimum length subarray - sliding window
2022-04-23 17:32:00 【hequnwang10】
One 、 Title Description
Given a containing n An array of positive integers and a positive integer target .
Find the sum of the array ≥ target The smallest length of Continuous subarray [numsl, numsl+1, …, numsr-1, numsr] , And return its length . If there is no sub array that meets the conditions , return 0 .
Example 1:
Input :target = 7, nums = [2,3,1,2,4,3]
Output :2
explain : Subarray [4,3] Is the smallest subarray in this condition .
Example 2:
Input :target = 4, nums = [1,4,4]
Output :1
Example 3:
Input :target = 11, nums = [1,1,1,1,1,1,1,1]
Output :0
Two 、 Problem solving
The sliding window
This problem uses a sliding window , First use the right border to move right , When finding a match and greater than target When subscript value of , Then move the left boundary . Indent left boundary , Find the shortest interval
class Solution {
public int minSubArrayLen(int target, int[] nums) {
// The idea of sliding windows
int left = 0,right = 0,length = nums.length;
int sum = 0;
int minLen = Integer.MAX_VALUE;
if(length == 0){
return 0;
}
while(right<length){
sum += nums[right];
while(sum >= target){
// Move left border
minLen = Math.min(minLen,right-left+1);
sum -= nums[left];
left++;
}
right++;
}
return minLen == Integer.MAX_VALUE?0:minLen;
}
}
Time complexity :O(n), among n Is the length of the array . The pointer start and end Up to each movement n Time .
Spatial complexity :O(1).
版权声明
本文为[hequnwang10]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231732009559.html
边栏推荐
- Basic case of Baidu map
- Advantages and disadvantages of several note taking software
- . net cross platform principle (Part I)
- Read software engineering at Google (15)
- Perception of linear algebra 2
- Come out after a thousand calls
- Some problems encountered in recent programming 2021 / 9 / 8
- Understanding of RPC core concepts
- How to manually implement the mechanism of triggering garbage collection in node
- 练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)
猜你喜欢

394. 字符串解码-辅助栈
![[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof](/img/71/14a17128dbe0f02edb4db3da479ef2.jpg)
[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof

.Net Core3. 1 use razorengine NETCORE production entity generator (MVC web version)

【WPF绑定3】 ListView基础绑定和数据模板绑定

为什么有些人说单片机简单,我学起来这么吃力?
![[ES6] promise related (event loop, macro / micro task, promise, await / await)](/img/69/ea3ef6063d373f116a44c53565daa3.png)
[ES6] promise related (event loop, macro / micro task, promise, await / await)
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]

Qt 修改UI没有生效

01 - get to know the advantages of sketch sketch

958. 二叉树的完全性检验
随机推荐
Low code development platform sorting
Qt 修改UI没有生效
JS to find the character that appears three times in the string
常用SQL语句总结
How to sort the numbers with text in Excel from small to large instead of the first number
Shell-sed命令的使用
01 - get to know the advantages of sketch sketch
ASP. Net core reads the configuration file in the class library project
C# Task. Delay and thread The difference between sleep
Abnormal resolution of Xiaomi camera
386. 字典序排数(中等)-迭代-全排列
開期貨,開戶雲安全還是相信期貨公司的軟件?
Compare the performance of query based on the number of paging data that meet the query conditions
Learning record of uni app dark horse yougou project (Part 2)
C dapper basically uses addition, deletion, modification and query transactions, etc
tidb-server 的配置文件在哪里?
开期货,开户云安全还是相信期货公司的软件?
练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)
Generating access keys using JSON webtoken
【WPF绑定3】 ListView基础绑定和数据模板绑定