Largest in every window
Given a list and a window size k, find the largest value in each window
of k consecutive items. A direct way is to take max of every slice.
Another way uses a deque of positions: before adding a new position,
drop positions from the back whose values are smaller than the new value,
since they can never be the maximum while the new one is in the window;
then add the new position at the back. Drop the position at the front if
it has fallen out of the window. The front is always the window's maximum.
Write window_max(nums, k) that returns the list of maximums. k is at
least 1 and at most the length of the list. Either method is acceptable.
window_max([1, 3, -1, -3, 5, 3, 6, 7], 3) -> [3, 3, 5, 5, 6, 7]
from collections import deque
def window_max(nums, k):
...