Skip to contentKEEP THE CRAFTTHE SANCTUARY

Maintain Sliding-Window Maximums

Find the maximum of every window as it slides, using a deque of indexes whose values decrease.

The problem

Implement window_maximums(values, window_size). A window is window_size neighboring numbers of values. Slide the window from the left end to the right end one step at a time and return the maximum of each window, in order. values has between 1 and 100,000 numbers, each between -10,000 and 10,000, and 1 <= window_size <= len(values). Aim for O(n) time.

Starter code

def window_maximums(values, window_size):
    pass

Public examples

  1. values = [1, 3, -1, -3, 5, 3, 6, 7], window_size = 3gives[3, 3, 5, 5, 6, 7]
  2. values = [4, 2, 9], window_size = 1gives[4, 2, 9]
  3. values = [-8, -2, -5, -1], window_size = 2gives[-2, -2, -1]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.