Putting the index on the stack instead of the value gives you two things at once: the value (values[i]) and the place. In questions like "how many days to wait?" the answer is the difference of two places; had you stored the value you could not work it out.
from collections import dequewindow = deque()for i, v in enumerate(values):while window and values[window[-1]] <= v:window.pop()window.append(i)if window[0] <= i - size:window.popleft()
deque does either end in one step; the window's largest is always at window[0].A stack uses one end (last in, first out), a queue both (first in, first out). pop(0) at the front of a list shifts every remaining element; when you need a queue, use a deque.