Visualized DSA Strategies

Episode 5 Sliding Window

59 min · JavaScript · Watch on YouTube · Quiz

Do you need this episode?

4 quick questions on what it teaches. Get them all right and skip ahead; miss one and you'll see the part of the video that answers it.

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

Quiz: test yourself

8 questions: the ones from the end of the video, then new code. Pick an answer to see why it's right.