Loading section...
Monotonic Deque: Sliding Window Max/Min in O(n)
Concepts: pyMonotonicDeque, pySlidingWindowMax, pySlidingWindowMin
This is the advanced intersection of sliding window and monotonic stacks. The problem: given an array and window size k, find the maximum element in every window of size k. Brute force: O(n*k). Heap: O(n log k). Monotonic deque: O(n). The deque maintains indices of candidate maximum elements in decreasing order. Elements outside the window are evicted from the front. Elements smaller than the incoming element are evicted from the back (they can never be the maximum of any future window while this larger element exists). The deque front always holds the index of the current window's maximum. Full Implementation with Proof of Correctness Proof of Correctness (State It Out Loud) The deque maintains two invariants simultaneously. First, all indices in the deque are within the current window (g