Problem 127
Daily Temperatures
Medium · 20 pts · Array, Stack, Monotonic Stack
Problem
You are given the temperature for each of N days. For every day, find how many days you would have to wait until a strictly warmer day. If no warmer day comes later, the answer for that day is 0. Use a stack of the indices of days that have not yet found a warmer day. For each new day, pop every index whose temperature is lower than today's and set its answer to the difference in days; then push today's index.
Input
First line: integer N. Second line: N space-separated integers.
Output
Print N values, space-separated.
Constraints
1 <= N <= 100000; 30 <= T[i] <= 100
Example input
8 73 74 75 71 69 72 76 73
Example output
1 1 4 2 1 1 0 0
Explanation
For 73 74 75 71 69 72 76 73: 73 waits 1 day for 74, 74 waits 1 day for 75, and 75 waits 4 days until 76. 71 waits 2 days for 72, 69 waits 1 day, 72 waits 1 day for 76, and 76 and the last 73 never see a warmer day. So the answer is 1 1 4 2 1 1 0 0.