Visualized DSA Strategies
Episode 5 Sliding Window
▶ This video is coming to YouTube soon.
The complete guide to the sliding window. First the theory: what a window is, fixed vs variable windows, the template behind every sliding window problem, and why a while loop inside a for loop is still O(n). Then nine types of window, each worked as a small problem in full: examples, the brute force animated beside the running line of code, the complete code, every line explained, and the sliding window solution the same way.
What you'll learn
- What a window is, and fixed vs variable windows
- The fixed and variable window templates, line by line
- Why each number entering once and leaving once makes it O(n)
- Fixed windows: a running sum and a running count
- Variable windows: the longest that obeys a rule, with a sum or a set
- The shortest window: shrink while it's still valid
- At most k kinds with a map, and counting every valid window
- Exactly k = at most k − at most k−1
- The window maximum with a monotonic deque
Chapters
- 0:00Intro
- 0:17What's a window?
- 0:44Fixed vs variable windows
- 1:11The fixed window template
- 1:57The variable window template
- 2:44Why it's O(n)
- 3:11What the window remembers
- 3:44Nine kinds of window
- 4:26Type 1 · Best sum of k in a row
- 10:27Type 2 · Most vowels in k letters
- 16:16Type 3 · Longest run with sum ≤ k
- 21:52Type 4 · Longest run with no repeated number
- 27:26Type 5 · Shortest run with sum ≥ target
- 33:09Type 6 · Longest run with at most 2 different numbers
- 39:01Type 7 · How many runs have sum ≤ k?
- 44:38Type 8 · Runs with exactly 2 odd numbers
- 50:22Type 9 · Max of every window of size k
- 56:16Spot the pattern
- 56:48Quiz: which window?
- 57:27Recap
Quiz: test yourself
The 4 questions from the end of the episode. Pick an answer to see why it's right.