One in, one out
transcript
A window of k values slides along a list. Recomputing it at every step is slow. It repeats work you already did. Keep a state instead.
Recomputing every window adds almost two hundred million numbers. A running total makes two hundred thousand steps.
Sum the first window once. Three plus one is four. Slide. Add the entering value, drop the leaving one. The next totals are five, five, and six. On a repeating roster, the last window wraps to the front. Eight.
Before the loop, ask what you keep about the window. Can one value in and one value out update it? If not, change the state, not the loop.
A total loses exactly the leaving value. Subtract it. A maximum may be the very one that leaves. Then you are stuck.
Choose what the window keeps. Let one in and one out update it. Keep each update constant time. Full breakdown in the lecture.