FunctionRacerProblemsRace

Highest Reading per Window

EasyStacks and queuesPython, JavaScript, TypeScript, Java, C++

readings is a list of sensor values and k is a window size. Look at every block of k consecutive readings, starting at the left end and moving one position at a time until the block touches the right end. Return a list with the highest value in each block, in order.

The result has len(readings) - k + 1 items. k is never larger than the number of readings.

Example 1

readings [1,3,-1,-3,5,3,6,7]
k 3
returns [3,3,5,5,6,7]

The blocks are [1,3,-1], [3,-1,-3], [-1,-3,5], [-3,5,3], [5,3,6] and [3,6,7].

Example 2

readings [4,2,9,1]
k 2
returns [4,9,9]

The blocks of two are [4,2], [2,9] and [9,1].

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Stacks and queues problems