Sliding Window
Sliding Window
Fixed, variable and frequency windows over contiguous ranges.
Sliding Window (Fixed Size)
▶ viz
Maintain an aggregate over every length-k contiguous subarray by adding the entering element and removing the leaving one, turning O(n·k) into O(n).
O(n) · O(1) space
Sliding Window (Variable Size)
▶ viz
Grow a window from the right while a condition holds and shrink it from the left when it breaks, finding the longest or shortest valid contiguous subarray in O(n).
O(n) · O(1) space
Sliding Window with Frequency Map
▶ viz
Slide a window over a string while a hash map tracks character counts and a "formed" counter says how many required characters are satisfied — the engine behind anagram search and minimum window substring.
O(n + m) · O(Σ) space