STAQ-P6

Biggest in each window

Practice 6 of 6Stacks and queues9 tests

The problem

A window of k values slides along the list a, one place at a time. Return the biggest value in each window, from the first window to the last. There are len(a) - k + 1 windows.

window_max([4, 2, 12, 3, 8, 5, 1, 7], 3)
# [12, 12, 12, 8, 8, 7]
window_max([5, 3, 8], 1)    # [5, 3, 8]

Limits

  • Up to 200,000 values. k is at least 1 and at most len(a). k can be large, so O(n × k) (n times k) is too slow.

Builds on STAQ-6 Monotonic queue.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

9 tests, not run yet
Test results
#TestResultReads · time
1The lesson's listNot run
2k = 1Not run
3k = nNot run
4Going downNot run
5Going upNot run
6RepeatsNot run
7Negative valuesNot run
8200,000 values, k = 50,000Not run
9200,000 going down, k = 1,000Not run