Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

nums =
k =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

Maintain a monotonic deque of indices whose values are in decreasing order.

Approach

For each index i:

  • Pop from the back while the new value is larger (it dominates smaller ones).
  • Push i.
  • Pop from the front if it’s outside the window.
  • The front is the max for the current window.

Why it works

The deque always contains only candidates for the maximum, ordered so the max is at the front.

Code
Loading...
Complexity
Time: O(n)
Space: O(k)
Solution
Updated: 2026-02-23

Idea

Maintain a monotonic deque of indices whose values are in decreasing order.

Approach

For each index i:

  • Pop from the back while the new value is larger (it dominates smaller ones).
  • Push i.
  • Pop from the front if it’s outside the window.
  • The front is the max for the current window.

Why it works

The deque always contains only candidates for the maximum, ordered so the max is at the front.

Code
Loading...
Complexity
Time: O(n)
Space: O(k)