Dynamic Programming21 sections · 916 units
Open in Course

Monotonic Queue - Walkthrough

Tracing the operations

Find sliding window maximum for [1,3,1,3,5,3][1, 3, -1, -3, 5, 3] with k=3k = 3. Window 1: indices [0,1,2]. Insert 1, deque=[1].

Insert 3, pop 1 (smaller), deque=[3]. Insert -1, deque=[3,-1]. Max=3. Window 2: indices [1,2,3]. Index 0 was already popped when value 3 was inserted. Deque starts as [3,-1]. Insert -3, deque=[3,-1,-3]. Max=3. Continue: window 3 inserts 5, clears everything smaller. Window 4 adds 3 after 5. Final maxes: [3, 3, 5, 5].