medium +25 pts

Daily Temperatures

Compute days until a warmer temperature for each day.

You are given a list `temperatures` of integers, where `temperatures[i]` is the temperature on day `i`. For each day, you need to find the number of days you have to wait until a warmer temperature appears. If there is no future day with a warmer temperature, the answer for that day is `0`. Write a function `daily_temperatures(temperatures: list[int]) -> list[int]` that returns a list of integers of the same length as the input, where the value at index `i` is the number of days until a warmer temperature (i.e., the difference in indices) or `0` if no warmer day exists. **Implementation requirements:** - The function must be named `daily_temperatures` and take exactly one argument: a list of integers. - The return value must be a list of integers. - You should use the input list as-is; do not modify it. **Constraints:** - `1 <= len(temperatures) <= 10^5` - Each temperature is an integer between `30` and `100` inclusive. - The time complexity should be O(n) or better in the average/worst case. **Note:** There is no need to read input or print output. The function will be called directly with test cases.

Constraints

1 <= len(temperatures) <= 100000; each temperature in [30, 100].

Example

>>> daily_temperatures([73, 74, 75, 71, 69, 72, 76, 73])
[1, 1, 4, 2, 1, 1, 0, 0]
>>> daily_temperatures([30, 40, 50, 60])
[1, 1, 1, 0]
>>> daily_temperatures([90, 80, 70, 60])
[0, 0, 0, 0]
25 points ~25 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Process the list from right to left, keeping track of indices with warmer temperatures.
Use a stack that stores indices of days with decreasing temperatures from bottom to top.
When you find a warmer day, pop from the stack until the stack top is warmer; then the difference is the answer.
If the list is strictly decreasing, the answer is all zeros.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.