滑動視窗是一種技巧,用來在陣列或字串上維護一段連續的子區間。透過「右邊擴展、左邊收縮」來避免重複計算,將暴力的 O(n²) 優化到 O(n)。
Notes:
- 視窗大小固定或可變,取決於題目條件
- 常搭配 Hash Map 來追蹤視窗內的元素狀態
- 關鍵在於判斷何時收縮左邊界
跨倉庫導讀#
- 對應理論章節:線性資料結構 ↗(陣列雙指針技法)
#3
Longest Substring Without Repeating Characters
#76
Minimum Window Substring
#121
Best Time to Buy and Sell Stock
#209
Minimum Size Subarray Sum
#219
Contains Duplicate II
#239
Sliding Window Maximum
#424
Longest Repeating Character Replacement
#567
Permutation in String
#658
Find K Closest Elements
#904
Fruits into Basket
#1343
Number of Sub Arrays of Size K and Avg Greater than or Equal to Threshold
#1456
Maximum Number of Vowels in a Substring of Given Length
#1658
Minimum Operations to Reduce X to Zero
#1838
Frequency of The Most Frequent Element
#1888
Minimum Number of Flips to Make The Binary String Alternating