当前位置:网站首页>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
边栏推荐
- Promise (II)
- 1-3 components and modules
- [difference between Oracle and MySQL]
- Webapi + form form upload file
- Solution of Navicat connecting Oracle library is not loaded
- ECMAScript history
- ASP. Net core dependency injection service life cycle
- [logical fallacy in life] Scarecrow fallacy and inability to refute are not proof
- Qt 修改UI没有生效
- ASP. NET CORE3. 1. Solution to login failure after identity registers users
猜你喜欢
练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)
Perception of linear algebra 2
ClickHouse-表引擎
基于51单片机红外无线通讯仿真
PC电脑使用无线网卡连接上手机热点,为什么不能上网
For the space occupation of the software, please refer to the installation directory
Exercise: even sum, threshold segmentation and difference (two basic questions of list object)
Further study of data visualization
Understanding of RPC core concepts
Advantages and disadvantages of several note taking software
随机推荐
C语言函数详解
[WPF binding 3] listview basic binding and data template binding
剑指 Offer 22. 链表中倒数第k个节点-快慢指针
Tdan over half
JVM类加载机制
Clickhouse - data type
ASP. NET CORE3. 1. Solution to login failure after identity registers users
给 el-dialog 增加拖拽功能
Preliminary understanding of promse
嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?
01 - get to know the advantages of sketch sketch
Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
Model problems of stock in and stock out and inventory system
Qt 修改UI没有生效
XTask与Kotlin Coroutine的使用对比
Use of todesk remote control software
Router object, route object, declarative navigation, programmed navigation
PC电脑使用无线网卡连接上手机热点,为什么不能上网
ECMAScript history
uni-app黑马优购项目学习记录(下)