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
DifficultyPatternProblemKey Insight
EasyfixedLC 121 ยท Best Time to Buy and Sell StockTrack min price so far, max profit at each step. O(n).
EasyfixedLC 643 ยท Maximum Average Subarray IFixed-size window of length k. Update sum in O(1) per slide.
MediumvariableLC 3 ยท Longest Substring Without Repeating CharactersExpand right, shrink left on duplicate. HashMap or HashSet. O(n).
MediumvariableLC 209 ยท Minimum Size Subarray SumShrink when sum reaches the target to minimize the window. O(n).
MediumvariableLC 424 ยท Longest Repeating Character ReplacementWindow size โˆ’ max freq โ‰ค k. Expand right, shrink left. O(n).
MediumfixedLC 438 ยท Find All Anagrams in a StringFixed-size window, frequency match. O(n).
MediumvariableLC 567 ยท Permutation in StringFixed-size window with frequency comparison. O(n).
MediumvariableLC 904 ยท Fruit Into BasketsAt most 2 distinct elements โ†’ longest subarray. O(n).
MediumvariableLC 1004 ยท Max Consecutive Ones IIITrack zero count and keep the longest window with at most k zeroes. O(n).
HardvariableLC 76 ยท Minimum Window SubstringExpand to cover all chars, shrink to minimise. O(n).
HardmonotonicLC 239 ยท Sliding Window MaximumMonotonic deque tracks max in fixed window. O(n).

โ† Back to all LeetCode categories