Lesson 7: Sliding window
Picture a small frame sliding along a row of items, showing only a few at a time: slide it right and one item leaves while one enters, most of what's inside stays. That's a 'sliding window'. Instead of re-summing every frame from scratch (O(n·k), slow), you just add what entered and subtract what le
A sliding window is like looking out the window of a moving train: at each moment you see almost the same view, only one tree slips away on one side and a new tree appears on the other. You don't need to look at the whole view again from scratch — just notice what left and what entered.
- sliding window
- A pattern where a contiguous range moves over an array, updating a running result by adding the entering element and subtracting the leaving one — instead of recomputing each window.
- fixed-size window
- A sliding window whose length k stays constant: each step adds one element and removes one.
- variable-size window
- A sliding window whose bounds grow and shrink according to a condition (e.g. a minimum sum), not a preset fixed length.