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:
The brute force approach to solving the daily temperatures problem involves checking every single day against all the future days that follow it. For each day on your calendar, you look ahead one day at a time until you finally spot a day that is warmer.
Here's how the algorithm would work step-by-step:
def daily_temperatures(temperature_list):
answer_list = [0] * len(temperature_list)
# Iterate through every single day to establish a baseline for finding future warmer temperatures
for current_day_index in range(len(temperature_list)):
day_counter = 1
target_day_index = current_day_index + 1
# Scan forward day by day until a warmer temperature is found or the end of the list is reached
while target_day_index < len(temperature_list):
if temperature_list[target_day_index] > temperature_list[current_day_index]:
answer_list[current_day_index] = day_counter
break
day_counter += 1
target_day_index += 1
# If no warmer day exists in the future, the answer remains zero as initialized
return answer_listInstead of checking every future day for every single point in time, we can look at the temperatures from right to left while keeping a handy waiting room for days that haven't found their warmer day yet. Whenever we see a warmer temperature, we match it up with the waiting days that are colder, figuring out their wait time instantly.
Here's how the algorithm would work step-by-step:
def daily_temperatures(temperature_list):
total_days = len(temperature_list)
answer_list = [0] * total_days
waiting_room = []
# Traverse backwards to instantly resolve future warmer days without redundant checks.
for current_day_index in range(total_days - 1, -1, -1):
current_temperature = temperature_list[current_day_index]
# Pop colder days from the stack since they have found their warmer future day.
while waiting_room and temperature_list[waiting_room[-1]] <= current_temperature:
waiting_room.pop()
# If days remain in the stack, the top one represents the next warmer temperature.
if waiting_room:
answer_list[current_day_index] = waiting_room[-1] - current_day_index
waiting_room.append(current_day_index)
return answer_list| Case | How to Handle |
|---|---|
| Empty or null input array | Return an empty array immediately to prevent null pointer exceptions or out-of-bounds errors. |
| Single-element array | Return an array containing a single zero since there are no future days to wait for. |
| Temperatures in strictly decreasing order | The monotonic stack naturally leaves these elements waiting indefinitely, resulting in an array of all zeros. |
| Temperatures in strictly increasing order | Each element will immediately pop the stack, resulting in an array where every day has a wait time of exactly one. |
| All identical temperature values | The stack will accumulate all indices without finding a greater value, leading to an array of all zeros. |
| Negative temperatures and mixed zero values | Since the logic relies entirely on relative numerical comparisons rather than absolute magnitudes, negative values and zeros are processed seamlessly. |
| Maximum-sized input arrays near constraints | The monotonic stack processes each element at most twice, ensuring optimal O(N) time complexity and preventing timeout issues. |
| Extreme boundary values for temperatures | Using standard 32-bit integers prevents overflow issues since we only store array indices and compare values. |