Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead.
Example 1:
Input: temperatures = [73,74,75,71,69,72,76,73] Output: [1,1,4,2,1,1,0,0]
Example 2:
Input: temperatures = [30,40,50,60] Output: [1,1,1,0]
Example 3:
Input: temperatures = [30,60,90] Output: [1,1,0]
Constraints:
1 <= temperatures.length <= 10530 <= temperatures[i] <= 100When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
For each day's temperature, we will look at all the following days, one by one, to find the first day that is warmer. This is like a simple but thorough search where we check every single possibility for each day.
Here's how the algorithm would work step-by-step:
def daily_temperatures_brute_force(temperatures):
number_of_days = len(temperatures)
answer_list = [0] * number_of_days
# Iterate through each day to find a warmer future day for it.
for current_day_index in range(number_of_days):
current_day_temperature = temperatures[current_day_index]
# Search for a warmer temperature in the subsequent days.
for future_day_index in range(current_day_index + 1, number_of_days):
future_day_temperature = temperatures[future_day_index]
# If a warmer day is found, calculate the day difference and stop searching for this current day.
if future_day_temperature > current_day_temperature:
days_to_wait = future_day_index - current_day_index
answer_list[current_day_index] = days_to_wait
break
return answer_listThe best way to solve this is to go through the days backwards, from the last day to the first. This approach cleverly keeps track of past warmer days, allowing us to quickly find the next warm day for the current day we are looking at without re-scanning everything.
Here's how the algorithm would work step-by-step:
def dailyTemperatures(temperatures):
number_of_days = len(temperatures)
days_until_warmer_temperature = [0] * number_of_days
# Iterate backwards to use previously calculated waiting times for future days.
for current_day_index in range(number_of_days - 2, -1, -1):
next_day_index = current_day_index + 1
# Keep jumping to the next warmer day until a day warmer than the current day is found.
while next_day_index < number_of_days and temperatures[next_day_index] <= temperatures[current_day_index]:
# If there's no warmer day for the next day, there won't be one for the current day either.
if days_until_warmer_temperature[next_day_index] == 0:
next_day_index = number_of_days
break
next_day_index += days_until_warmer_temperature[next_day_index]
# If a warmer day was found within bounds, calculate the distance.
if next_day_index < number_of_days:
days_until_warmer_temperature[current_day_index] = next_day_index - current_day_index
return days_until_warmer_temperature| Case | How to Handle |
|---|---|
| An empty input list | The function should return an empty list as there are no temperatures to process. |
| A list with a single temperature | The result list should contain a single zero, as there are no future days to compare against. |
| Temperatures are in strictly decreasing order (e.g., [75, 74, 73]) | The output list should be all zeros since no day is followed by a warmer day. |
| Temperatures are in strictly increasing order (e.g., [73, 74, 75]) | Each element in the output list will be 1, as every day is followed by a warmer day immediately. |
| All temperatures in the list are identical (e.g., [70, 70, 70]) | The output list will consist entirely of zeros because no temperature is ever warmer than a previous one. |
| Input list contains the maximum number of elements (e.g., 100,000) | A monotonic stack approach with O(N) time complexity is required to avoid a timeout that would occur with a naive O(N^2) solution. |
| Input contains the minimum and maximum possible temperature values (e.g., 30 and 100) | The solution must correctly handle calculations and comparisons across the full valid range of temperature values. |
| A list with a single large spike in temperature at the end (e.g., [70, 60, 50, 80]) | The monotonic stack correctly pops all previous colder temperatures to find their next warmer day at the final spike. |