23/36
Part 6 of 8 · The monotonic stackLearn and do

After the pops, the top is the nearest earlier value the new one didn't beat

The same stack answers the mirror question too. Take the 4 in [5, 3, 1, 4, 2, 6]. It pops 1 and 3, and the 5 is left under it. Every value between the 5 and the 4 was beaten and popped, so the 5 is the nearest earlier value the 4 didn't beat.

So next larger reads its answers off the items it pops. Previous larger reads its answer off the item that survives.

The same shape answers how many days until a warmer day, how far back a price's run goes, and how much rain water bars can hold. The rain water comes up in the follow-ups.

In the editor, write spans(prices) with the value that survives on top. For each day, it gives how many days in a row, ending that day, had a price at most that day's price.