The window maximum in one pass
transcript
A stream of numbers arrives. A window of three slides along. Find the biggest in each window. Checking each one is slow. A deque does it faster.
The deque keeps positions, biggest first. Twelve arrives. Smaller values leave the back. Twelve is at the front. It is the maximum. Eight arrives. Three leaves the back. Twelve slides out. It leaves the front.
Four windows give four answers. Twelve, twelve, twelve, and eight. Each answer is read from the front. Reading it costs one step.
A new value arrives later. It stays in the window longer. So a smaller value behind it is useless. Drop it from the back for good.
Every value goes on once. And comes off at most once. So the work is at most two n. Not n times the window size.
A monotonic deque keeps order. The front is the maximum. One pass does it all. Try it in the sandbox. Full breakdown in the lecture.