Resolve pending answers with a monotonic stack
For each temperature, find how many days pass before a strictly warmer day. Keeping all unanswered days on a stack avoids restarting a search for every day. The stack holds indices with non-increasing temperatures. A new warmer day resolves as many pending days as it can, and every index is pushed and popped at most once.
pending = [0, 1]
temperatures = [20, 18, 22]
print(2 - pending[-1])Think before coding
The stack stores questions whose answers are still unknown. Popping an index means the current day is its first warmer day. Although there is a nested loop, no index can be popped twice, giving linear total work. Work through the example by hand before changing the starter. Decide what should happen for an empty input, a single value and a repeated value where those cases apply. The checks use several inputs so a hard-coded answer cannot stand in for the rule.
Your task
Write warmer_days(temperatures). Return the wait to a strictly warmer future temperature for each position, or zero if none arrives. Equal temperatures do not count as warmer.
The explanation opens here once every check passes.