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:
| Window | How we got it | Sum | Best |
|---|---|---|---|
| 2, 1, 5 | added 3 numbers | 8 | 8 |
| 1, 5, 1 | drop 2, add 1 | 7 | 8 |
| 5, 1, 3 | drop 1, add 3 | 9 | 9 |
| 1, 3, 2 | drop 5, add 2 | 6 | 9 |
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.
k- k = 3
- nums = [2, 1, 5, 1, 3, 2]
- n = len(nums)
- best = None
- if k <= n:
- s = sum(nums[:k])
- best = s
- for r in range(k, n):
- s += nums[r] - nums[r-k]
- best = max(best, s)
- print(best)
Variables
- k
- 3 (just changed)
Each window holds k days in a row.
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.
- s = "pwwkew"
- seen = set()
- left = 0
- best = 0
- for r in range(len(s)):
- while s[r] in seen:
- seen.remove(s[left])
- left += 1
- seen.add(s[r])
- best = max(best, r-left+1)
- print(best)
Variables
- s
- "pwwkew" (just changed)
Find the longest stretch with no repeat.
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.
| Window | Time | Extra space |
|---|---|---|
| Fixed size k | O(n) | O(1) |
| Variable, with a set | O(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.
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.