We have a list of daily temperatures and, for each day, we need to figure out how many days until a strictly warmer temperature occurs. If no warmer day exists in the future, the answer for that day is 0.
A direct scan works: for each day, look forward until something warmer appears. But many days can share the same next warmer day. When temperatures fall for several days and then spike, that one spike is the answer for every day in the falling stretch. An algorithm that resolves all of those days at once, the moment the spike arrives, avoids the repeated scanning. A stack of unresolved days supports this: accumulate days as they pass, then pop everything cooler when a warmer day shows up.
1 <= temperatures.length <= 10^5: with up to 100,000 elements, an O(n^2) brute force can reach 10^10 operations in the worst case. We need O(n) or O(n log n).30 <= temperatures[i] <= 100: temperatures span only 71 distinct values. The space-optimized approach later in this chapter relies on this narrow range for its runtime bound.Do what the problem describes: for each day, look at every future day until one is warmer, and record the distance. If the scan reaches the end without finding one, the answer stays 0.
This is correct but does a lot of repeated scanning. If temperatures decrease for a long stretch and then spike, every day in that stretch scans all the way to the spike independently.
i from 0 to n-1:j from i+1 to n-1:temperatures[j] > temperatures[i], set answer[i] = j - i and break.With n up to 10^5, the O(n^2) worst case is too slow. The waste is the repeated scanning: many days scan to the same warmer day independently. The next approach accumulates unresolved days and resolves them in batches the moment a warmer day appears.
Instead of having each day search forward for its answer, flip the perspective: process days left to right and maintain a stack of days that have not yet found a warmer future day. The stack holds indices, and the temperatures at those indices are in non-increasing order from bottom to top (a monotonic decreasing stack, where equal temperatures sit together).
When a new day arrives, compare it against the index on top of the stack. If today is warmer, today is the answer for that stacked day: pop it and record the distance. Keep popping as long as today is warmer than the stack's top, because today resolves all of those cooler days. Then push today onto the stack.
The invariant: temperatures at stacked indices are non-increasing from bottom to top. It holds because before day i is pushed, every index with a temperature strictly below temperatures[i] has been popped, so everything still on the stack is at least as warm as day i. Equal temperatures stay stacked together, since a day with the same temperature does not count as warmer.
The popped answers are correct because the stack contains every index that has not yet found a warmer day, in order. When day i pops index prevDay, no day between them was warmer than prevDay (any such day would have popped it earlier), so i is the nearest warmer day.
i from 0 to n-1:temperatures[i] > temperatures[stack.top]:prevDay from the stack.answer[prevDay] = i - prevDay.i onto the stack.The monotonic stack is O(n) in time, which is optimal, but it uses O(n) extra space for the stack. The final approach removes the stack by processing days from right to left and using the answer array itself to skip ahead.
Processing days from right to left means the answers for all future days are already computed, and each one can serve as a jump pointer.
For day i, check day i+1. If it is warmer, the answer is 1. If not, answer[i+1] gives the distance to the next warmer day after i+1, so jump to i + 1 + answer[i+1] and check that day instead. Keep jumping until a warmer day appears, or until a day with answer = 0 is reached, which means no warmer day exists from that point forward.
This removes the stack entirely. The answer array doubles as the navigation structure.
The answer array forms a chain of next-warmer links: answer[j] = k means the next warmer day after j is at j + k, and every day strictly between them has a temperature no greater than temperatures[j] (otherwise answer[j] would point to that closer day instead).
Skipping those intermediate days is safe when temperatures[j] <= temperatures[i]: the skipped days are at most as warm as j, and j is not warm enough for i, so none of them can be warm enough for i either. The first day in the jump sequence with a temperature above temperatures[i] is therefore the nearest warmer day.
n-2 down to 0 (the last day's answer is always 0):j = i + 1.temperatures[j] <= temperatures[i]:answer[j] == 0, no warmer day exists from j onward, so answer[i] = 0. Break.j = j + answer[j].temperatures[j] > temperatures[i], set answer[i] = j - i.j to j + answer[j] lands on a strictly warmer day, so the temperatures visited while resolving one day form a strictly increasing sequence. With values limited to 30 through 100, that sequence has at most 71 entries, so each day performs at most 70 jumps. Without a bounded value range, the bound is O(n * k), where k is the number of distinct temperature values.