02
Sliding Window
Contiguous subarrays and substrings.
5 pages
- Pattern 02: Sliding WindowPattern guide
There are exactly two window templates. Decide first: is the window size fixed or variable? Everything else follows.
- LC 209Minimum Size Subarray Sum (LC 209)Medium
Expand the window until the sum is large enough, then shrink from the left to minimize length while the sum stays valid.
- LC 643Maximum Average Subarray I (LC 643)Easy
Maintain a fixed window of size k, slide by adding/removing one element at a time
- LC 219Contains Duplicate II (LC 219)Easy
Maintain a window of k elements using a HashSet to detect duplicates. Problem walkthrough from the Sliding Window pattern set — Java templates, complexity…
- LC 1004Max Consecutive Ones III (LC 1004)Medium
Expand the right edge of the window; track how many zeros are inside; when zeros exceed k, shrink from the left until valid again. The maximum window length…