Sliding Window
Templates
def slidingWindow(s: str):
window = ...
left, right = 0, 0
while right < len(s):
# c 是将移入窗口的字符
c = s[right]
window.add(c)
# 增大窗口
right += 1
# 进行窗口内数据的一系列更新
...
# print(f"window: [{left}, {right})")
# 判断左侧窗口是否要收缩
while left < right and window needs shrink:
# d 是将移出窗口的字符
d = s[left]
window.remove(d)
# 缩小窗口
left += 1
# 进行窗口内数据的一系列更新
...
Question types
Fixed size sliding window
- Find the size of the window required, say K.
- Compute the result for 1st window, i.e. include the first K elements of the data structure.
- Then use a loop to slide the window by 1 and keep computing the result window by window.
Variable size sliding window
- In this type of sliding window problem, we increase our right pointer one by one till our condition is true.
- At any step if our condition does not match, we shrink the size of our window by increasing left pointer.
- Again, when our condition satisfies, we start increasing the right pointer and follow step 1.
- We follow these steps until we reach to the end of the array.
l = 0
cache = {}
maxLen = 0
for r in range(length):
while s[r] is True:
cache.remove(s[l])
l += 1
cache.add(s[r])
maxLen = max(maxLen, r - l + 1)
return maxLen
Double Ended Queue
- Record the index of the element in the queue
- When the index exceeds k, pop from the front
- Also need to ensure the queue is in a decreasing order
Remove duplicates with fast slow pointer
- When condition satisfies (Such as there is no duplicate), swap the element at fast pointer to slow pointer position
- When the condition fail (Such as there exist a duplicate), only move the fast pointer forward
80. Remove Duplicates from Sorted Array II
Method | Related Questions |
---|---|
Variable length sliding window: - Increment r while condition True - Decrement l while condition False - Update max |
3. Longest Substring Without Repeating Characters, 2461. Maximum Sum of Distinct Subarrays With Length K |
Fixed length sliding window: - Compute short length first - Compare once first - Iterate through the solutions then |
567. Permutation in String |
Max double ended queue - Keep the maximum at the front of the queue - Remove from the queue if it is smaller than newly append element |
239. Sliding Window Maximum |
File | confidence | date | difficulty | note |
---|---|---|---|---|
239. Sliding Window Maximum | 🟡 | April 01, 2024 | - | - |
1838. Frequency of the Most Frequent Element | 🔴 | July 29, 2024 | medium | Variable size sliding window, compare the window total + k to window length * target value |
1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit | 🔴 | October 31, 2024 | medium | Two deque, record the max and min at each position, pop out of it if outside of the limit |
121. Best Time to Buy and Sell Stock | 🟡 | November 11, 2024 | - | - |
File | confidence | date | difficulty | note |
---|---|---|---|---|
209. Minimum Size Subarray Sum | 🟢 | March 03, 2024 | medium | - |
567. Permutation in String | 🟢 | March 17, 2024 | - | - |
424. Longest Repeating Character Replacement | 🟢 | March 17, 2024 | - | - |
53. Maximum Subarray | 🟢 | March 26, 2024 | - | - |
1004. Max Consecutive Ones III | 🟢 | March 28, 2024 | - | - |
2461. Maximum Sum of Distinct Subarrays With Length K | 🟢 | January 11, 2025 | medium | - |