Sliding Window Maximum
Netherfield Power reports demand every minute and publishes a rolling peak: for each period of a fixed number of consecutive minutes, the highest demand seen in that period. The periods start at the first minute and shift along by one minute each time, so consecutive periods overlap heavily. Periods that would run past the end of the day are not reported, and neither are the short ones at the start before a full period exists.
Task: Print the highest demand in each period as the period slides along.
Input
Two lines. The first holds between 1 and 100000 integers separated by single spaces — the demand each minute, in time order, possibly negative. The second holds the period length k, between 1 and the number of readings.
Output
One line holding the peaks separated by single spaces, in time order: first the highest of readings 1 to k, then the highest of readings 2 to k+1, and so on until the period's last minute is the final reading. Only full periods of exactly k minutes are reported.
Example:
Input:
1 3 -1 -3 5 3 6 7
3
Output:
3 3 5 5 6 7
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in