Rolling maximum over a sliding window
Asked at Two Sigma, Optiver
For a price series and a window size , report the maximum price in every window of consecutive ticks as the window slides one step at a time.
prices = [1, 3, -1, -3, 5, 3, 6, 7], k = 3
-> [3, 3, 5, 5, 6, 7]
Return the list of window maxima. Rescanning each window is ; aim for .
Show a hint
If an older price is smaller than a newer one still inside the window, the older price can never be a future maximum. What if you kept only the "still relevant" prices, in decreasing order?
Your answer
This one is open-ended. Work it through, then check your reasoning against the full solution.