Maintained by Vikas Dulgunde, software engineer
You are handed a list of daily high temperatures in the order the days occurred. For every position you want to know the distance to the next day that is hotter than the current one.
The answer for a day is the number of positions you have to move forward before you reach a temperature that is strictly greater. A reading that is equal does not count as warmer, so ties are skipped over.
If a day has no warmer day anywhere ahead of it, its answer is 0. Build and return a new list of the same length holding these wait counts.
Day 0 (73) warms up the very next day (74), so 1. Day 2 (75) waits until index 6 (76), a gap of 4. Days 6 and 7 never see anything hotter ahead, so both are 0.
Each day is immediately followed by a hotter one, giving a wait of 1, except the last day which has nothing after it.
Temperatures only fall, so no day ever has a warmer day ahead and every answer is 0.
Visible test cases
The brute-force answer for index i is just a forward scan: walk right until you find a bigger value and record the distance. Get that working first.
Notice that once a later, hotter day resolves an earlier waiting day, that earlier day never needs to be looked at again. Can you keep the still-unresolved days somewhere?
Hold the indices of days that are still waiting for a warmer day on a stack, kept in decreasing-temperature order. When today is hotter than the day on top, today resolves it: pop and record the index difference.
For each day, scan every later day until you hit one that is warmer, then store that distance.
Keep a stack of indices whose warmer day has not been found yet, with their temperatures decreasing from bottom to top. The current day resolves every shallower day it beats in one pass.