当前位置:网站首页>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
边栏推荐
- XTask与Kotlin Coroutine的使用对比
- Solution of Navicat connecting Oracle library is not loaded
- 【生活中的逻辑谬误】稻草人谬误和无力反驳不算证明
- Webapi + form form upload file
- [difference between Oracle and MySQL]
- Simulation of infrared wireless communication based on 51 single chip microcomputer
- [logical fallacy in life] Scarecrow fallacy and inability to refute are not proof
- 基于51单片机红外无线通讯仿真
- How to sort the numbers with text in Excel from small to large instead of the first number
- ClickHouse-数据类型
猜你喜欢
![[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)

双指针进阶--leetcode题目--盛最多水的容器

394. 字符串解码-辅助栈

Understanding of RPC core concepts
![[WPF binding 3] listview basic binding and data template binding](/img/2e/fbdb4175297bb4964a8ccfd0b909ae.png)
[WPF binding 3] listview basic binding and data template binding

Exercise: even sum, threshold segmentation and difference (two basic questions of list object)

Summary of common SQL statements

. net type transfer
![Using quartz under. Net core - [1] quick start](/img/80/b99417e88d544ca6e3da4c0c1625ce.png)
Using quartz under. Net core - [1] quick start

ASP. NET CORE3. 1. Solution to login failure after identity registers users
随机推荐
Basic case of Baidu map
超分之TDAN
给 el-dialog 增加拖拽功能
ClickHouse-数据类型
[WPF binding 3] listview basic binding and data template binding
剑指 Offer 03. 数组中重复的数字
stm32入门开发板选野火还是正点原子呢?
1-3 components and modules
How does matlab draw the curve of known formula and how does excel draw the function curve image?
JVM class loading mechanism
Use of shell awk command
[difference between Oracle and MySQL]
Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
Self use learning notes - connectingstring configuration
C语言程序设计之函数的构造
开期货,开户云安全还是相信期货公司的软件?
Signalr can actively send data from the server to the client
Change Oracle to MySQL
Preliminary understanding of promse
uni-app黑马优购项目学习记录(下)