当前位置:网站首页>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
边栏推荐
- ASP. NET CORE3. 1. Solution to login failure after identity registers users
- HCIP第五次实验
- Seven cattle upload pictures (foreground JS + background C API get token)
- Generating access keys using JSON webtoken
- . net type transfer
- JS, entries(), keys(), values(), some(), object Assign() traversal array usage
- Router object, route object, declarative navigation, programmed navigation
- [C] thoroughly understand the deep copy
- 440. 字典序的第K小数字(困难)-字典树-数节点-字节跳动高频题
- [difference between Oracle and MySQL]
猜你喜欢

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

Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory

RPC核心概念理解

. net type transfer

48. 旋转图像

Understanding of RPC core concepts

超分之TDAN

1217_使用SCons生成目标文件

01-初识sketch-sketch优势

Learning record of uni app dark horse yougou project (Part 2)
随机推荐
El date picker limits the selection range from the current time to two months ago
[C] thoroughly understand the deep copy
Qt 修改UI没有生效
The system cannot be started after AHCI is enabled
C listens for WMI events
Summary of common SQL statements
1-2 JSX syntax rules
【WPF绑定3】 ListView基础绑定和数据模板绑定
Baidu Map Case - Zoom component, map scale component
Use of shell sed command
How to sort the numbers with text in Excel from small to large instead of the first number
Wiper component encapsulation
快时钟同步慢时钟域下的异步控制信号slow clk to fast clk
Some problems encountered in recent programming 2021 / 9 / 8
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
JS, entries(), keys(), values(), some(), object Assign() traversal array usage
古代埃及希腊,数学用的什么进制
Scope and scope chain in JS
Webapi + form form upload file
Net standard