Core Idea: Maintain a window (subarray/substring) and expand/shrink it based on conditions. Avoids recomputing from scratch (O(n²) → O(n)).
Variable Size: Expand right, shrink left when constraint violated. Track max/min length.
Fixed Size: Slide window of size k, add new element, remove old element.
Goal: Find max profit from buying low and selling high (buy before sell).
Why Sliding Window? For each sell day, we need the minimum buy price before it. Instead of scanning left each time (O(n²)), track running minimum as we go. Window is implicitly [minBuy...currentDay].
Key Insight: At each position, we have two choices: (1) sell at current price (profit = price - minBuy), or (2) update minBuy if current is lower. No need to track window explicitly.
Simplest Pattern:
Track single state (minimum/maximum) as window expands.Goal: Find length of longest substring with all unique characters.
Why Sliding Window? Brute force checks all substrings O(n²). With sliding window: expand right adding chars, shrink left when duplicate found. Each char processed at most twice (add once, remove once) = O(n).
Key Insight: Use set to track chars in current window. When new char already exists, shrink from left until it's removed. Window always contains unique chars.
Trap:
Shrink loop must delete chars until duplicate removed, not just once.Goal: With at most k replacements, find longest substring of same character.
Why Sliding Window? Window is valid if: (window_length - max_freq) ≤ k. The chars NOT matching the most frequent char are what we'd replace. Expand right, shrink left when replacements exceed k.
Key Insight: Track frequency of each char in window. Chars to replace = windowLen - maxFreq. If this exceeds k, shrink window. maxFreq can be stale (not decremented) because we only care about finding longer valid windows.
Key Formula:
replacements_needed = window_length - max_frequencyGoal: Check if s2 contains any permutation of s1 as substring.
Why Sliding Window? Permutation = same character counts in different order. Use fixed window of size len(s1). Slide through s2, comparing frequency arrays. If they match, permutation found.
Key Insight: Window size is fixed (len(s1)). Maintain frequency count of current window. Slide by adding new char, removing old char. Compare arrays in O(26) = O(1).
Fixed Window:
Size never changes. Only slide, don't expand/shrink.Goal: Find shortest substring of s containing all chars of t.
Why Sliding Window? Expand right until window contains all chars of t. Then shrink left to find minimum. Track "letters complete" to know when all t chars are satisfied.
Key Insight: Track two things: (1) frequency of each t char needed, (2) how many distinct chars have reached required count. When lettersComplete == len(tCount), window is valid. Shrink to find minimum.
Two Counts:
Track both required frequency and "satisfied" count.Goal: Return max element in each window of size k.
Why Deque? Naive approach: for each window, find max = O(nk). Deque maintains indices in decreasing order of values. Front is always the max. When new element comes, remove all smaller from back (they can never be max).
Key Insight: Deque stores potential maximums. When sliding: (1) remove front if out of window, (2) remove all back elements smaller than new (they're useless), (3) add new to back. Front is current max.
Store Indices:
Store indices in deque, not values, to check window bounds.Use When: Find longest/shortest subarray satisfying condition.
Examples: Longest Substring Without Repeating, Minimum Window Substring.
Template: Expand j, shrink i when invalid, track result at each step.
Use When: Window size is given (size k), or determined by input (permutation).
Examples: Permutation in String, Sliding Window Maximum.
Template: Initialize window, then slide: add right, remove left.
Use When: Need running min/max or character frequencies.
Examples: Best Time Buy/Sell (min), Char Replacement (maxFreq).
Key: Update state incrementally, don't recompute from scratch.
Use When: Need min/max in sliding window efficiently.
Examples: Sliding Window Maximum.
Key: Maintain decreasing (for max) or increasing (for min) order.
Common Thread:
All avoid O(n²) by maintaining window state incrementally. Expand/shrink or slide the window, updating state in O(1) per step.