当前位置:网站首页>sliding window
sliding window
2022-06-26 11:00:00 【Code Xiaobai】
Basic concepts
Sliding window is an idea based on double pointers , A window is formed between the elements pointed to by the two pointers .
Application scenarios
About the problem
1、 Generally, the data structure given is an array or a string
2、 When finding the longest and shortest value of a substring or subsequence, or finding a target value
3、 The problem itself can be solved by violence
keyword
Satisfy XXX Conditions ( The result of the calculation is , Number of occurrences , Also include )
The shortest / The longest
Substring / Subarray / Subsequence
for example : Subarray with the smallest length
Slide window idea
Find the longest
—— The core : Left and right hands (L,R) At the starting point ,R Slide the cycle bit by bit to the right
—— During each slide
If : The elements in the window meet the conditions ,R Expand the window to the right , And update the optimal results
If : The elements in the window do not meet the conditions ,L Narrow the window to the right
——R To the end
Find the shortest
—— The core : Left and right hands (L,R) At the starting point ,R Slide the cycle bit by bit to the right
—— During each slide
If : The elements in the window meet the conditions ,L Narrow the window to the right , And update the optimal results
If : The elements in the window do not meet the conditions ,R Expand the window to the right
——R To the end
The code template
Longest template
// Longest template
initialization left,right,result,bestResult
while( The right pointer does not reach the end ){
The window expands , Join in right The corresponding element , Update current result
while(result Not meeting the requirements ){
The window shrinks , remove left The corresponding element ,left Move right
}
Update the best results bestResult
right++;
}
return bestResult
Shortest template
// Shortest template
initialization left,right,result,bestResult
while( The right pointer does not reach the end ){
The window expands , Join in right The corresponding element , Update current result
while(result Meet the requirements ){
Update the best results bestResult
The window shrinks , remove left The corresponding element ,left Move right
}
right++;
}
return bestResult
leetcode Example
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 .
link :https://leetcode.cn/problems/minimum-size-subarray-sum
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
Tips :
1 <= target <= 109
1 <= nums.length <= 105
1 <= nums[i] <= 105
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int left=0,right=0,minLength=0,sum=0;
while (right<nums.length){
sum=sum+nums[right];
while (sum>=target){
if (right-left+1<minLength||minLength==0){
minLength=right-left+1;
}
sum=sum-nums[left];
left++;
}
right++;
}
return minLength;
}
}
result :
边栏推荐
- Reasons for "unresolved external symbols" during vs or QT compilation link:
- 携程机票 App KMM 跨端 KV 存储库 MMKV-Kotlin | 开源
- DataBinding使用与原理分析
- 互联网对抗神器之漏洞扫描与反渗透
- JWT certification agreement -- I opened a Yihong hospital
- Recent work report
- ISO 26262 - 2 functional safety concept
- See how I store integer data in the map < string, string > set
- CentOS安装Redis多主多从集群
- MySQL backup and restore command
猜你喜欢

Alibaba cloud OSS - object storage service (tool)

ACK攻击是什么意思?ACK攻击怎么防御?

Enter a positive integer with no more than 5 digits, and output the last digit in reverse order

Basic MySQL

nacos2.x.x启动报错信息Error creating bean with name ‘grpcClusterServer‘;

Swiftui development experience: data layer of application design for offline priority

Sqli labs range 1-5

工程数学概率论统计简明教程第二版复习大纲

看我在Map<String, String>集合中,存入Integer类型数据

4、 Stacks and queues
随机推荐
ceph运维常用指令
QT连接MySql数据查询失败
Jasperreports - print PDF (project tool)
Sqli labs range 1-5
即构「畅直播」上线!提供全链路升级的一站式直播服务
Oracle sqlplus 查询结果显示优化
Nuxt. JS - learning notes
Postman入门教程
jwt认证协议阐述之——我开了一家怡红院
RDB持久化验证测试
[work details] March 18, 2020
近期工作汇报
Will updating a large amount of data in Oracle cause the undo space to explode
開發者,微服務架構到底是什麼?
Bit operation n & (n-1), leetcode231, interview question 05.06
Docker中实现MySQL主从复制
[echart] II. User manual and configuration item reading notes
02-Redis数据结构之链表
Sqli-labs靶场1-5
8- creating leecode algorithm with pictures and texts - algorithm solution of minimum stack and LRU caching mechanism