Back
Monotonic Deque
Question 1 of 128% Complete
Medium
Window minimum using an increasing deque of indices
Mirror of sliding window maximum with reversed inequality.
from collections import deque
 
def minSlidingWindow(nums, k):
dq = deque()
out = []
for i, x in enumerate(nums):
while dq and dq[0] <= i - k:
dq.popleft()
while dq and nums[dq[-1]] >= x:
dq.pop()
dq.append(i)
if i >= k - 1:
out.append(nums[dq[0]])
return out
Need a hint?
For minimum, pop while back is greater or equal to current.