当前位置:网站首页>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
边栏推荐
- 【生活中的逻辑谬误】稻草人谬误和无力反驳不算证明
- MySQL installation
- Construction of functions in C language programming
- JS failed to change all variables and changed to the return method. Finally, the problem was solved
- Clickhouse - data type
- Advantages and disadvantages of several note taking software
- 线性代数感悟之1
- 读《Software Engineering at Google》(15)
- 嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?
- 01-初识sketch-sketch优势
猜你喜欢
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
SiteServer CMS5. 0 Usage Summary
【WPF绑定3】 ListView基础绑定和数据模板绑定
Advantages and disadvantages of several note taking software
[difference between Oracle and MySQL]
PC电脑使用无线网卡连接上手机热点,为什么不能上网
线性代数感悟之1
C语言函数详解
Tdan over half
STM32 entry development board choose wildfire or punctual atom?
随机推荐
Shell-入门、变量、以及基本的语法
Open futures, open an account, cloud security or trust the software of futures companies?
Promise (IV)
开期货,开户云安全还是相信期货公司的软件?
STM32 entry development board choose wildfire or punctual atom?
node中,如何手动实现触发垃圾回收机制
matlab如何绘制已知公式的曲线图,Excel怎么绘制函数曲线图像?
Using quartz under. Net core - calendar of [6] jobs and triggers
剑指 Offer 22. 链表中倒数第k个节点-快慢指针
Understanding of RPC core concepts
剑指 Offer 03. 数组中重复的数字
PC uses wireless network card to connect to mobile phone hotspot. Why can't you surf the Internet
How to manually implement the mechanism of triggering garbage collection in node
uni-app黑马优购项目学习记录(下)
ASP. Net core configuration options (Part 2)
Deep understanding of control inversion and dependency injection
C语言函数详解
Halo 开源项目学习(二):实体类与数据表
Clickhouse SQL operation
Using quartz under. Net core - [1] quick start