当前位置:网站首页>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 :
边栏推荐
- Sqli labs range 1-5
- AdaptiveAvgPool2D 不支持 onnx 导出,自定义一个类代替 AdaptiveAvgPool2D
- Sqli-labs靶场1-5
- Vscode environment setup: synchronous configuration
- Jasperreports - print PDF (project tool)
- June training (the 26th day) - collective search
- appliedzkp zkevm(8)中的Plookup Table
- 2、 Linear table
- 9、 Beautify tables, forms, and hyperlinks
- 代码规范 & 详细解释 husky、prettier、eslint、lint-staged 的作用和使用
猜你喜欢

Easyx-----c语言实现2048

Win10 start FTP service and set login authentication

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

Basic MySQL

携程机票 App KMM 跨端 KV 存储库 MMKV-Kotlin | 开源

Redis knowledge mind map

Quantitative investment learning - Introduction to classic books

Sqli labs range 1-5

MySQL 8th job

基础-MySQL
随机推荐
MySQL backup and restore command
SwiftUI 开发经验之为离线优先的应用程序设计数据层
The difference between NPM and yarn
MySQL模糊查询详解
Function run time
Developers, what is the microservice architecture?
appliedzkp zkevm(8)中的Plookup Table
Mysql 30条军规
Grain Mall - High Availability Cluster
Swiftui development experience: data layer of application design for offline priority
搜索引擎高级搜索方法记录
The sixth MySQL job - query data - multiple conditions
2020.7.6 interview with fence network technology company
Grain Mall - distributed Foundation
MySQL 12th job - Application of stored procedure
2021 Q3-Q4 Kotlin Multiplatform 使用现状 | 调查报告
即构「畅直播」上线!提供全链路升级的一站式直播服务
Easyx-----c语言实现2048
AIX basic operation record
Using reflection to export entity data to excel