All problems

Lowest Reading Blocks

Medium · stack

A sensor logs one reading per day, in the list readings. Readings can be negative.

For each day i, look at the longest run of consecutive days that contains day i and in which no reading is smaller than readings[i]. Call it the block of day i. Readings equal to readings[i] are allowed inside the block, and the block can be just day i on its own.

Return a list with one number per day: the sum of the readings in the block of that day.

Examples

Example 1

Input: readings = [3, 1, 4, 1, 5]

Output: [3, 14, 4, 14, 5]

Day 1 (reading 1) can stretch over the whole list, since nothing is smaller than 1, so its block sums to 14. Day 3 also reads 1 and gets the same block. Day 0 (reading 3) is stopped by the 1 next to it, so its block is just itself.

Example 2

Input: readings = [2, 2, 2]

Output: [6, 6, 6]

Equal readings do not stop a block, so every day's block is the whole list.

Example 3

Input: readings = [-1, 4, -3, 2]

Output: [3, 4, 2, 2]

Day 0 (reading -1) reaches over the 4 but is stopped by -3, giving -1 + 4 = 3. Day 2 (reading -3) covers everything, and the sum is 2.

Constraints