Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 51s1 days 16 hours 1 minutes 51 seconds.
Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 51s1 days 16 hours 1 minutes 51 seconds.

Sign in to watch this lesson
Algorithms - Arrays · 4:45
A stack keeps its minimum in O(1) by caching the minimum below every element, but a queue removes from the other end. We slide a window of 3 over [1, 3, -1, -3, 5, 3, 6, 7] with a monotone deque whose values climb like a staircase: newer, smaller values bulldoze older ones off the back and expired indices fall off the front, so every window minimum is read from the front in amortized O(1). Then we build a queue from two min-stacks, which works for any associative operation such as gcd or OR, and measure the rescan against the deque on a million values.