Fill the blank
Fill the blank so the front of the deque is always the span maximum.
from collections import deque
counts, k = [1, 3, -1, -3, 5, 3, 6, 7], 3
dq: deque[int] = deque()
out = []
for i, c in enumerate(counts):
while dq and counts[dq[-1]] ____ c:
dq.pop()
dq.append(i)
if dq[0] <= i - k:
dq.popleft()
if i >= k - 1:
out.append(counts[dq[0]])
print(out)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A <=
Right: an older count that is not bigger can never be a future maximum.
Popping weaker candidates from the back keeps the deque in decreasing order, so its front is the answer for each span.
Why not the others?
- B >=. That throws away the larger, more useful candidates.
- C ==. Only removing equal values leaves smaller ones that break the order.