WorkspaceProblem lab
ALGORITHM VISUALIZER

Sliding Window Maximum

Opening the interactive workspace…

Understanding Sliding Window Maximum

Return the maximum value in each contiguous window of exactly k values, in left-to-right order.

How it works

A deque keeps useful candidate indices in decreasing value order, with the maximum at its front.

Expire indices that have left the window. Remove smaller or equal values from the back before adding the new index. Read the front value once the window has k elements.

Try an example

Input: nums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3

Output: [3, 3, 5, 5, 6, 7]

A deque keeps useful candidate indices in decreasing value order, with the maximum at its front.

Constraints

  • 1–64 integers between -1,000,000 and 1,000,000.
  • k is an integer from 1 through nums.length.

Use the simulator

Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.

Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.

Related concepts and complexity