Likou brushing questions notes: sliding window method python template writing (detailed annotations, widely used, seckill sliding window questions)

Sliding window template:

In the book "Challenge Programming Contest", the sliding window is called "the insect method", which is very vivid. Because the movement of the two pointers of the sliding window is very similar to the crawling process of a bug: if the front foot does not move, move the back foot over; if the back foot does not move, move the front foot forward.

Sharing a sliding window template can solve most sliding window problems:

Sliding window template python code:

def findSubArray(nums):
    N = len(nums) # 数组/字符串长度
    left, right = 0, 0 # 双指针,表示当前遍历的区间[left, right],闭区间
    sums = 0 # 用于统计 子数组/子区间 是否有效,根据题目可能会改成求和/计数
    res = 0 # 保存最大的满足题目要求的 子数组/子串 长度
    while right < N: # 当右边的指针没有搜索到 数组/字符串 的结尾
        sums += nums[right] # 增加当前右边指针的数字/字符的求和/计数
        while 区间[left, right]不符合题意:# 此时需要一直移动左指针,直至找到一个符合题意的区间
            sums -= nums[left] # 移动左指针前需要从counter中减少left位置字符的求和/计数
            left += 1 # 真正的移动左指针,注意不能跟上面一行代码写反
        # 到 while 结束时,我们找到了一个符合题意要求的 子数组/子串
        res = max(res, right - left + 1) # 需要更新结果
        right += 1 # 移动右指针,去探索新的区间
    return res

The left and right pointers are used in the sliding window. The idea of ​​their movement is to use the right pointer as a drive and drag the left pointer to move forward. The right pointer only moves one step at a time, while the left pointer may move multiple steps at a time in the inner while loop. The right pointer actively moves forward to explore new unknown areas; the left pointer is forced to move and is responsible for finding the interval that satisfies the meaning of the question.

The overall idea of ​​the template:

Define two pointers left and right to point to the beginning and end of the interval respectively, note that it is a closed interval; define sums to count the number of occurrences of each character in the interval;

The first while loop is to determine whether the position of the right pointer exceeds the array boundary; each time right reaches a new position, the sum/count of the right pointer needs to be increased;

The second while loop is to move the left pointer to the right to the position where the [left, right] interval meets the meaning of the question; when the left moves to a new position each time, the sum/count of the left pointer needs to be reduced;

After the second while loop, I successfully found a [left, right] interval that meets the meaning of the question. The question requires the maximum interval length, so we update res to max(res, the length of the current interval).

The right pointer moves one step to the right each time, starting to explore a new range.

The sums in the template need to be modified according to the meaning of the topic.

Another thing that needs to be modified according to the question is the judgment condition of the inner while loop, namely: the interval [left, right] does not meet the meaning of the question.

Typical examples:
1. Likou 1004. Maximum number of consecutive 1 III
https://blog.csdn.net/weixin_44414948/article/details/113862992
2. Likou 567. Arrangement of character strings
https://blog.csdn .net/weixin_44414948/article/details/113781406
3. Likou 992.K sub-array of different integers
https://blog.csdn.net/weixin_44414948/article/details/113771127
4. Likou 1423. The maximum obtainable Points
https://blog.csdn.net/weixin_44414948/article/details/113727700
5. Likou 995.K minimum number of consecutive flips
https://blog.csdn.net/weixin_44414948/article/details/113861118

Author: fuxuemingzhu
link: https://leetcode-cn.com/problems/max-consecutive-ones-iii/solution/fen-xiang-hua-dong-chuang-kou-mo-ban-mia-f76z/
Source: force LeetCode (LeetCode) https://leetcode-cn.com/problems/max-consecutive-ones-iii/

Guess you like

Origin blog.csdn.net/weixin_44414948/article/details/113862173