from collections import deque

def window_maximum(nums, k):
    if k < 1 or k > len(nums):
        return []
    candidates = deque()
    output = []
    for right, value in enumerate(nums):
        while candidates and candidates[0] <= right - k:
            candidates.popleft()
        while candidates and nums[candidates[-1]] <= value:
            candidates.pop()
        candidates.append(right)
        if right + 1 >= k:
            output.append(nums[candidates[0]])
    return output
