Complete the code to initialize the deque for sliding window maximum.
from collections import deque dq = [1]()
We use deque from collections to efficiently add and remove elements from both ends.
Complete the code to remove indices from the deque that are out of the current window.
if dq and dq[0] [1] i - k: dq.popleft()
We remove the index at the front if it is outside the current window, which means its index is less than or equal to i - k.
Fix the error in the loop that removes smaller elements from the deque.
while dq and nums[dq[-1]] [1] nums[i]: dq.pop()
We remove indices from the deque while the current number is greater than the numbers at those indices to maintain decreasing order.
Fill both blanks to append the maximum for each window to the result list.
if i [1] k - 1: result.[2](nums[dq[0]])
We start adding maximums to the result list once the first full window is processed (i >= k - 1). We use append to add the maximum.
Fill all three blanks to complete the sliding window maximum function.
def maxSlidingWindow(nums, k): from collections import deque dq = deque() result = [] for i in range(len(nums)): if dq and dq[0] [1] i - k: dq.popleft() while dq and nums[dq[-1]] [2] nums[i]: dq.pop() dq.append(i) if i [3] k - 1: result.append(nums[dq[0]]) return result
The first blank removes indices out of the window (less than or equal to i - k), the second blank removes smaller elements (<), and the third blank checks if the window is full (i >= k - 1) to append the maximum.