Search⌘ K
AI Features

Problem: Sliding Window Maximum

Explore the sliding window maximum problem to understand how to efficiently find the maximum element in every window of size k across an array. Learn to implement a monotonic deque that keeps track of indices in decreasing order, allowing you to retrieve maximums in linear time. This lesson helps you apply queue data structures to solve real-world problems while analyzing the time and space complexity of your solution.

Statement

Given an array of integers nums and an integer k representing the size of a sliding window, the window starts at the leftmost position of the array and moves one position to the right at each step until it reaches the rightmost position. At each position, the window covers exactly k consecutive elements.

Return an array containing the maximum value within the sliding window at each position.

Note: The result array will have exactly nums.length k+1- k + 1 elements, one for each valid window position.

Constraints:

  • 11 \leq nums.length 105\leq 10^5

  • ...