Algorithms / Sliding window technique

Sliding window technique

How to answer questions about every run of neighbours in a list in one pass: keep a window, and adjust its total as it moves instead of adding everything again.

Beginner7 steps~6 min
Cell — one item of the list
Window — the run of cells being looked at
Sum — running total, adjusted on every move
L and R — left and right ends of the window
// THE SHORT VERSION

In 30 seconds

Many problems ask about runs of neighbouring items: the best three days in a row, the shortest stretch that reaches a total, the longest piece of text with no repeated letter.

A sliding window is a frame over part of the list. When it moves, one item leaves and one enters, so you update the running answer instead of recomputing it.

The window can have a fixed width, or it can grow on the right and shrink on the left as a rule demands. Either way each item is visited only a couple of times.

Sliding window technique, step by step

STEPS01 / 07

The grow-and-shrink method for sums assumes no negative numbers: only then does adding a cell never lower the sum and dropping one never raise it.

STEP 01 / 07 The slow way
THE MATH
array: 3 1 2 6 4 1 k = 3 4 windows × 3 cells = 12 reads
  1. Step 1: The slow way. Which three neighbours in this row add up to the most? The obvious method adds up every group of three from scratch. Neighbouring groups share two of their three cells, so most of that adding is repeated work.
  2. Step 2: Put a window on it. Lay a frame over the first three cells and add them once. This frame is the window. Its width is fixed at k = 3, and the only thing we remember about it is one number: its sum.
  3. Step 3: Slide: update, not redo. Move the frame one cell to the right. One number leaves on the left and one enters on the right; the two in the middle stay. So take the old sum, subtract what left and add what entered: two operations, however wide the window is.
  4. Step 4: Slide to the end. Keep sliding and note the largest sum seen: 12, for cells 2 to 4. The whole row is handled in one pass. With a million numbers and k = 1000, adding every group from scratch reads about a billion cells; sliding does about two million small steps.
  5. Step 5: A window that grows. New question: what is the shortest run of neighbours whose sum reaches 10? Now the width is not given. Use two markers, L and R, for the window’s ends. Move R right, adding each new cell, until the sum is at least 10. That happens at cell 3, with a window of length 4.
  6. Step 6: Shrink from the left. While the window is valid, note its length and drop its leftmost cell. Once the sum falls below 10, move R again. Dropping 3 breaks it; adding 4 repairs it; then 1 and 2 can both go. The shortest valid window is 6, 4, length 2. L and R each only move forward, so this is still one pass.
  7. Step 7: The same idea on text. A string is a row of letters, so the window works there too. To find the longest stretch with no repeated letter, grow on the right and, when a letter appears twice, shrink from the left until it is unique again. In abcabd that gives cabd, length 4. Fixed or variable, the pattern is the same.
VALUES SO FAR
array3 1 2 6 4 1input
k3input
sum 0..2····start
sum 1..3····update
best····result
target····rule
shortest····result
longest····result

Spotted a mistake on this page?