Please enable Javascript to view the contents

Algo Review: sliding window

 ·  ☕ 1 分钟 · 👀... 阅读

A review on basic operation of sliding window.

Base template

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
def sliding_window(s: str):
    window = ... # int/map/etc.

    left, right = 0, 0
    while right < len(s):
        # enlarge window
        window.add(s[right])
        right += 1

        while left. < right and window.need_shrink():
            window.remove(s[left])
            left += 1
            # update result
您的鼓励是我最大的动力