当前位置:网站首页>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
边栏推荐
- Detailed explanation of C webpai route
- ASP. Net core dependency injection service life cycle
- Using quartz under. Net core -- a simple trigger of [7] operation and trigger
- [ES6] promise related (event loop, macro / micro task, promise, await / await)
- C dapper basically uses addition, deletion, modification and query transactions, etc
- Learning record of uni app dark horse yougou project (Part 2)
- 386. 字典序排数(中等)-迭代-全排列
- Use of todesk remote control software
- 嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?
- Shell-sed命令的使用
猜你喜欢
Net standard
48. 旋转图像
RPC核心概念理解
Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
ASP. Net core JWT certification
Webapi + form form upload file
How to change input into text
[ES6] promise related (event loop, macro / micro task, promise, await / await)
2. Electron's HelloWorld
EF core in ASP Generate core priority database based on net entity model
随机推荐
stm32入门开发板选野火还是正点原子呢?
给 el-dialog 增加拖拽功能
394. 字符串解码-辅助栈
. net type transfer
01 - get to know the advantages of sketch sketch
Use of shell sed command
Use of Shell sort command
Indexes and views in MySQL
MySQL installation
Further optimize Baidu map data visualization
Read software engineering at Google (15)
Some problems encountered in recent programming 2021 / 9 / 8
Exercise: even sum, threshold segmentation and difference (two basic questions of list object)
Webapi + form form upload file
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
48. 旋转图像
Double pointer advanced -- leetcode title -- container with the most water
Manually implement call, apply and bind functions
How does matlab draw the curve of known formula and how does excel draw the function curve image?
node中,如何手动实现触发垃圾回收机制