#K12511. Daily Temperatures
Daily Temperatures
Daily Temperatures
You are given n daily temperatures, where the temperature for the ith day is given by T[i]. Your task is to compute, for each day, the number of days you have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead.
More formally, for each day index i (where 0 \le i < n), find the smallest index j such that j > i and T[j] > T[i], and set the answer for day i to be j - i. If no such j exists, the answer is 0.
The problem can be mathematically represented as:
[
answer[i] = \begin{cases}
j - i, &\text{if } T[j] > T[i] \text{ for the smallest such } j
0, &\text{if no such } j \text{ exists}
\end{cases}
]
This problem tests your ability to efficiently process arrays and use data structures such as stacks.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains an integer n, the number of days.
- The second line contains n space-separated integers representing the daily temperatures.
outputFormat
Print to standard output (stdout) n space-separated integers where the ith integer is the number of days to wait until a warmer temperature. If no such day exists for a given day, output 0 for that day.
## sample8
73 74 75 71 69 72 76 73
1 1 4 2 1 1 0 0