Machine Room

Sliding Window

Find the k values in a row with the biggest total, in one pass: add the first window once, then slide it, one value out and one in. The one-page notes from the reel.

Download the PDF

One-page notes, Sliding Window. Find the k values in a row with the biggest total, in one pass. 1, The problem. A row of six boxes tagged sales: 40, 30, 50, 46, 39, 44, day 1 to day 6. Four brackets in a staircase, one for each run of three days next to each other, and under them total, then four boxes holding a question mark. Which 3 days in a row have the biggest total? Given an array and a window size k, find the k values next to each other with the biggest sum. The interview version: LeetCode 643, Maximum Average Subarray I. Every window holds k values, so the biggest sum is also the biggest average. 2, The slow way: add every run from scratch. 40 + 30 + 50 = 120. 30 + 50 + 46 = 126. 50 + 46 + 39 = 135. 46 + 39 + 44 = 129. The 30 + 50 in the first two lines is circled in both, marked added again. Runs next to each other share 2 of their 3 values, so most of the adding is repeated. n values, window k: (n − k + 1) windows, k − 1 additions each. Here 4 × 2 = 8. 3, The slide: one out, one in. The row with a green window over days 3 to 5 (50, 46, 39), an arrow marked slide; − 30 out under day 2, + 39 in under day 5. A table, slide, window, total, best so far: start, days 1-3, 40 + 30 + 50 = 120, 120. 1, days 2-4, 120 − 40 + 46 = 126, 126. 2, days 3-5, 126 − 30 + 39 = 135, 135. 3, days 4-6, 135 − 50 + 44 = 129, 135. Answer: days 3 to 5, with 50 + 46 + 39 = 135. 4, Why the width doesn't matter. new total = total − value out + value in. Seven boxes with a green window over boxes 2 to 6: box 1 marked − out, box 6 marked + in, boxes 2 to 5 stay in the total. Every slide costs 2 steps: one subtraction and one addition. The values in the middle stay in the total, so they are never added again, however wide the window is. The slow way adds every value of every window again. With 6 values it saves nothing: 8 additions against 8 steps. 5, In code. def best_window(a, k): total = sum(a[:k]); best, start = total, 0; for i in range(k, len(a)): total += a[i] - a[i - k]; if total > best: best, start = total, i - k + 1; return best, start. best_window([40, 30, 50, 46, 39, 44], 3) → (135, 2). Start 2 is the third value, counting from 0: days 3 to 5. Python's docs slide the same way (collections, deque recipes): s += elem - d.popleft(). 6, The cost. A table, slow way additions against sliding window steps: n = 6, k = 3: 8 and 8. n = 100,000, k = 50,000: 2,499,999,999 and 149,999. Time: O(n × k) and O(n). The window: (k − 1) + 2 × (n − k) steps. One full sum, then 2 a slide. At n = 100,000, about 16,667 times fewer steps. Memory: it keeps only a running total and the best so far. 7, A window that changes size. Eight boxes, a, b, c, a, b, c, b, b, with a green window over the first three: longest: "abc", 3. Both of its edges move right: the right edge grows, the left edge shrinks. Grow on the right. When a letter repeats, shrink from the left. Both ends only move right, so it is still one pass. LeetCode 3, Longest Substring Without Repeating Characters: "abcabcbb" → 3. Items next to each other? Don't start over each time. Slide the window.

On a phone, press and hold the image to save it.