Sliding window maximum
Return the maximum in every consecutive window of width k. 1 <= k <= len(items), except an empty input returns []. Use linear time.
from collections import deque
values = [1, 3, 2]
candidates = deque([1, 2])
print(values[candidates[0]])Reason about the state
A deque stores candidate indices in decreasing value order. Remove expired indices from the front and dominated values from the back. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Return the maximum in every consecutive window of width k. 1 <= k <= len(items), except an empty input returns []. Use linear time.
The explanation opens here once every check passes.