#K10501. Daily Temperature Span
Daily Temperature Span
Daily Temperature Span
You are given the number of days n
and a sequence of recorded daily temperatures. The task is to compute the temperature span for each day.
The span of the temperature on a given day is defined as the number of consecutive days (up to and including that day) for which the temperature was less than or equal to the temperature on that day. Formally, for day i (0-indexed), the span Si is defined as:
$$S_i = \begin{cases} i+1, & \text{if no previous day has a higher temperature};\\ i - j, & \text{where } j \text{ is the index of the previous day with a higher temperature} \end{cases}$$
Implement an efficient algorithm with a time complexity of \( O(n) \) to compute the spans for all days.
inputFormat
The input is provided from stdin and consists of two lines:
- The first line contains a single integer
n
representing the number of days. - The second line contains
n
space-separated integers representing the daily temperatures.
outputFormat
Output to stdout a single line containing n
space-separated integers, where the i-th
integer is the temperature span for the i-th
day.
7
100 80 60 70 60 75 85
1 1 1 2 1 4 6