LeetCode ยท Sliding Window
Sliding Window Problem Set
Fixed and variable-size windows over sequences. The key insight: expand right, shrink left, track state in a map or counter.
Concept page: Sliding Window
| Difficulty | Pattern | Problem | Key Insight |
|---|---|---|---|
| Easy | fixed | LC 121 ยท Best Time to Buy and Sell Stock | Track min price so far, max profit at each step. O(n). |
| Easy | fixed | LC 643 ยท Maximum Average Subarray I | Fixed-size window of length k. Update sum in O(1) per slide. |
| Medium | variable | LC 3 ยท Longest Substring Without Repeating Characters | Expand right, shrink left on duplicate. HashMap or HashSet. O(n). |
| Medium | variable | LC 209 ยท Minimum Size Subarray Sum | Shrink when sum reaches the target to minimize the window. O(n). |
| Medium | variable | LC 424 ยท Longest Repeating Character Replacement | Window size โ max freq โค k. Expand right, shrink left. O(n). |
| Medium | fixed | LC 438 ยท Find All Anagrams in a String | Fixed-size window, frequency match. O(n). |
| Medium | variable | LC 567 ยท Permutation in String | Fixed-size window with frequency comparison. O(n). |
| Medium | variable | LC 904 ยท Fruit Into Baskets | At most 2 distinct elements โ longest subarray. O(n). |
| Medium | variable | LC 1004 ยท Max Consecutive Ones III | Track zero count and keep the longest window with at most k zeroes. O(n). |
| Hard | variable | LC 76 ยท Minimum Window Substring | Expand to cover all chars, shrink to minimise. O(n). |
| Hard | monotonic | LC 239 ยท Sliding Window Maximum | Monotonic deque tracks max in fixed window. O(n). |