The Index That Left
Sliding window minimum with a monotonic deque of indices. One maintenance step is missing.
from collections import deque
def window_mins(a, k):
dq, out = deque(), []
for i, x in enumerate(a):
while dq and a[dq[-1]] >= x:
dq.pop()
dq.append(i)
if i >= k - 1:
out.append(a[dq[0]])
return out
Run it on a = [2, 1, 4, 5, 3, 4, 1, 2] with k = 4. Which window is the first one reported wrongly?
Sign in to answer questions and track your progress
Sign In