Every window after the first came from the one before: drop the number that left, add the one that joined. Moving a window of k numbers in a row like this is a sliding window.

Quick check

Quick check: The window moves from [1, 5, 1] to [5, 1, 3]. How does the sum change?

Why sliding saves work#

Think of the list as a hostel friend's daily steps, in thousands, over 6 days. You want the best 3-day total. Here is every window, and what each one cost:

WindowHow we got itSumBest
2, 1, 5added 3 numbers88
1, 5, 1drop 2, add 178
5, 1, 3drop 1, add 399
1, 3, 2drop 5, add 269

With n days and windows of k days, re-summing costs k additions for each of about n windows: O(n·k). Sliding pays k additions once, then one drop and one add per slide: O(n).

Quick check

Quick check: A fitness app stores 1,00,000 days of steps. You want the best 1,000-day total. Roughly how many additions and subtractions does each way do?

The fixed window, line by line#

s is the current window's sum and r is the day that joins. The day that leaves is r - k, exactly k places behind it. Line 9 does both at once: add nums[r], subtract nums[r-k]. Step through, then drag k to 6 and to 7.

Best total of k days in a rowline 1
Try another k
  1. k = 3
  2. nums = [2, 1, 5, 1, 3, 2]
  3. n = len(nums)
  4. best = None
  5. if k <= n:
  6. s = sum(nums[:k])
  7. best = s
  8. for r in range(k, n):
  9. s += nums[r] - nums[r-k]
  10. best = max(best, s)
  11. print(best)

Variables

k
3 (just changed)

Each window holds k days in a row.

Step 1 of 18

Quick check

Quick check: k is 6 and the list has 6 numbers. How many windows are there?

When the window size is not fixed#

Now find the longest substring (characters in a row) with no repeated character. The window has no fixed size: you grow it on the right one step at a time, and while it holds a repeat, you shrink it from the left. left is where the window starts and r is where it ends, so the window is s[left] to s[r]. A set seen holds the characters inside, so checking a repeat is O(1). Watch left move when the second w arrives.

Longest substring with no repeatline 1
  1. s = "pwwkew"
  2. seen = set()
  3. left = 0
  4. best = 0
  5. for r in range(len(s)):
  6. while s[r] in seen:
  7. seen.remove(s[left])
  8. left += 1
  9. seen.add(s[r])
  10. best = max(best, r-left+1)
  11. print(best)

Variables

s
"pwwkew" (just changed)

Find the longest stretch with no repeat.

Step 1 of 39

From index left to index r there are r-left+1 characters (from 2 to 4 is 3). The answer is 3, for wke (and kew). pwke does not count: its letters are not in a row.

Quick check

Quick check: Same code on s = "aaaa". What does it print?

Why the inner loop is still O(n)#

A loop inside a loop looks like O(n²). It is not, because left never moves back. r moves forward n times and left moves forward at most n times, so the two loops together run at most 2n steps.

WindowTimeExtra space
Fixed size kO(n)O(1)
Variable, with a setO(n)O(size of the set)

The set holds at most one copy of each character, so for lowercase letters it never holds more than 26.

Quick check

Quick check: s has 6 characters. Across the whole run, at most how many times can left += 1 run?

Same pattern, other rules#

Change the rule and the same grow and shrink works. You get a list of 0s and 1s and may change (flip) at most k zeros into ones. Find the longest run of ones you can make. A window works if it has at most k zeros, since you can flip those. Play it and watch the zero count: when one zero too many joins, the left end moves until a zero leaves.

Visualizer

Max Consecutive Ones III

Open full →

This works because shrinking can only fix a window: removing a character never creates a repeat, and removing a number never adds a zero.

One last check#

Quick check

Quick check: Run the longest-substring code on s = "abba". What does it print?

Next is the problem Longest Substring Without Repeating Characters: it is the grow and shrink window you just traced, now on any string you are given.