Skip to content
BytePatterns

Days Until Warmer

MediumStacks & Queues#monotonic-stack~30m

Problem

You are given daily temperature readings in order. For each day, report how many days you must wait before a strictly warmer reading appears. Write 0 for any day that is never followed by a warmer one.

Examples

Input:  temps = [30, 32, 31, 35]
Output: [1, 2, 1, 0]
Why:    day 0 waits one day, day 1 waits until day 3, day 3 never warms up
Input:  temps = [40, 39, 38]
Output: [0, 0, 0]
Why:    the readings only cool down
Input:  temps = [20]
Output: [0]
Why:    edge case, a single day has no future at all

Hints

0 / 3

Stuck on the idea rather than the code? Monotonic Stack covers it.