Arrays & Strings
Fixed-size and variable-size sliding window patterns
Arrays: Sliding Window
Active InputWindow size k = 3
Array
2
01
15
21
33
42
5Choose a pattern to animate the window one step at a time.
Two Sliding Window Patterns
1. Fixed-size Window keeps exactly k elements in view. After the first full window is built, each step removes one value from the left and adds one from the right.
2. Variable-size Window expands until a condition becomes true, then shrinks to find a tighter valid answer. This pattern is common for minimum-length or longest-valid-subarray problems.
Complexity
Fixed Window
Time:O(n)
Space:O(1)
Variable Window
Time:O(n)
Space:O(1)