Twelve problems that drill the sliding-window pattern from Lesson 4. The reps are the point — read the approach, try it yourself first, then check the code. Each solution here was actually run and tested.
How to practise. For each problem, first decide whether the window is fixed-size (slide by one, add the new element and drop the old) or variable-size (grow the right edge, shrink the left edge while a condition is violated). Write the loop, dry-run it on the example, and only then peek at the code. Re-solve from memory a day later.
The 12 problems
- 1Best Time to Buy and Sell Stock (LeetCode 121)EasyMax profit from one buy and one later sell: track the lowest price so far and the best gap above it.arrow_forward
- 2Maximum Average Subarray I (LeetCode 643)EasyFind the highest average of any k consecutive numbers: slide a fixed window, adding one and dropping one.arrow_forward
- 3Longest Substring Without Repeating Characters (LeetCode 3)MediumLength of the longest substring with all distinct characters: grow a window, jump its left edge past repeats.arrow_forward
- 4Longest Repeating Character Replacement (LeetCode 424)MediumLongest run of one letter after at most k replacements: a window is valid while length − top count ≤ k.arrow_forward
- 5Permutation in String (LeetCode 567)MediumDoes s2 contain a rearrangement of s1? Slide a window of len(s1) over s2 and compare letter counts.arrow_forward
- 6Find All Anagrams in a String (LeetCode 438)MediumReturn every start index where an anagram of p appears in s: slide a len(p) window and compare counts.arrow_forward
- 7Fruit Into Baskets (LeetCode 904)MediumLongest run of trees holding at most two fruit types: grow a window, shrink while it has three kinds.arrow_forward
- 8Max Consecutive Ones III (LeetCode 1004)MediumLongest run of 1s if you may flip up to k zeros: the longest window containing at most k zeros.arrow_forward
- 9Minimum Size Subarray Sum (LeetCode 209)MediumShortest subarray with sum at least target: grow until the sum is enough, then shrink while it still is.arrow_forward
- 10Longest Subarray of 1's After Deleting One Element (LeetCode 1493)MediumDelete exactly one element and return the longest run of 1s: a window with at most one zero, minus one.arrow_forward
- 11Sliding Window Maximum (LeetCode 239)HardMax of every window of size k: keep a deque of indices with decreasing values; the front is the max.arrow_forward
- 12Minimum Window Substring (LeetCode 76)HardShortest substring of s containing every character of t: grow until covered, then shrink from the left.arrow_forward