Sliding window algorithm
Principle
The idea of the sliding window algorithm is this:
We use the left and right pointer trick in the double pointer in string S, initialize left = right = 0, and call the index closed interval [left, right] a “window”.
We first keep increasing the right pointer to expand the window [left, right] until the string in the window meets the requirements (including all the characters in T).
At this point, we stop adding right, and instead keep increasing the left pointer to shrink the window [left, right] until the string in the window no longer meets the requirements (does not contain all the characters in T). At the same time, every time we increase left, we have to update the result one round.
Repeat steps 2 and 3 until the end of string S is reached to the right.
This idea is actually not difficult. ** The second step is equivalent to finding a “feasible solution”, and then the third step is to optimize this “feasible solution” and finally find the optimal solution. ** The left and right pointers take turns to advance, the window size increases or decreases, and the window keeps sliding to the right.
Example
Link to the topic: https://leetcode-cn.com/problems/minimum-window-substring/
Pseudocode
1 | string s, t; |
Concrete realization
1 | /** |
Maximum value that does not meet the conditions (Updated on 2021/02/19)
The sliding window algorithm above obtains the shortest length that meets the condition.
But sometimes we find the maximum length that does not meet the condition.
For example, today’s leetcode daily question
https://leetcode-cn.com/problems/max-consecutive-ones-iii/
** The meaning of the question is converted. Convert “You can turn at most K zeros into ones, and find the length of the longest subarray containing only ones” to “Find the longest subarray that allows at most K zeros” **
So in this problem, the condition is: ** subarray contains K or more 0 **.
In this case, our problem-solving template has changed
1 | string s, t; |
Careful observation can be found, in fact, the template does not change much, the most important change is the result of the place changed, from the inner loop to the outer loop.
Why is there such a change?
To understand this we need to really understand what the inner and outer cycles are doing?
The outer loop is constantly expanding the length of the subarray until the condition is met. ** In other words, the outer loop will iterate over all cases that do not meet the condition. **
The inner loop keeps shortening the length of the subarray until the condition is not met. ** In other words, the inner loop will iterate over all cases that meet the condition. **
Understood this, let’s solve this problem again, it’s not difficult, just go to the code
1 | var longestOnes = function(A, K) { |
Seeing here, some people may ask, you only said the shortest that meets the conditions and the longest that does not meet the conditions, so what about the longest that meets the conditions and the shortest that does not meet the conditions?
Regarding this issue, everyone can think about whether the last two are meaningful. The longest that meets the condition can directly judge whether the entire array meets the condition, because the longest that meets the condition can only be the entire array, and the entire array does not meet the condition, and the sub-array is even more impossible. The shortest that does not meet the condition is just to traverse one by one, and use each item in the array to judge whether it meets the condition.
In both cases, there is no need for sliding windows at all.