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 want to find out how many days we have to wait until a warmer temperature occurs. The brute force way to do this is to simply check every single day that comes after the current day.
Here's how the algorithm would work step-by-step:
def dailyTemperatures(temperatures):
number_of_days = len(temperatures)
waiting_days_result = [0] * number_of_days
# Iterate through each day's temperature to find its waiting period
for current_day_index in range(number_of_days):
current_day_temperature = temperatures[current_day_index]
days_to_wait = 0
found_warmer_day = False
# Check subsequent days for a warmer temperature
for lookahead_day_index in range(current_day_index + 1, number_of_days):
lookahead_day_temperature = temperatures[lookahead_day_index]
days_to_wait += 1
# If a warmer day is found, record the waiting period and stop searching for this day
if lookahead_day_temperature > current_day_temperature:
waiting_days_result[current_day_index] = days_to_wait
found_warmer_day = True
break
# If no warmer day was found after checking all subsequent days, the waiting period remains 0
if not found_warmer_day:
waiting_days_result[current_day_index] = 0
return waiting_days_resultWe want to find the number of days you need to wait for a warmer temperature. The clever approach uses a special structure to keep track of recent colder days. When a warmer day arrives, it can quickly tell how many days passed since the previously colder days.
Here's how the algorithm would work step-by-step:
def dailyTemperatures(temperatures):
number_of_days = len(temperatures)
days_to_wait_result = [0] * number_of_days
# Stack to store indices of days with temperatures that haven't found a warmer day yet.
indices_of_colder_days_stack = []
for current_day_index in range(number_of_days):
current_temperature = temperatures[current_day_index]
# While there are days in the stack and the current day is warmer than the day at the top of the stack.
while indices_of_colder_days_stack and current_temperature > temperatures[indices_of_colder_days_stack[-1]]:
previous_colder_day_index = indices_of_colder_days_stack.pop()
# Calculate and store the number of days until a warmer day.
days_to_wait_result[previous_colder_day_index] = current_day_index - previous_colder_day_index
# Push the current day's index onto the stack if it's not warmer than previous days or if the stack is empty.
indices_of_colder_days_stack.append(current_day_index)
return days_to_wait_result| Case | How to Handle |
|---|---|
| Empty input array temperatures | An empty input array should result in an empty output array. |
| Input array with a single element | A single element array should result in an output array with a single zero, as there are no future days. |
| All temperatures are the same | If all temperatures are identical, the output array should contain all zeros because no warmer day will ever occur. |
| Temperatures are strictly decreasing | If temperatures are strictly decreasing, no warmer day exists for any given day, so the output array should be all zeros. |
| Temperatures are strictly increasing | If temperatures are strictly increasing, the wait for each day will be exactly 1. |
| Temperatures contain negative values or zero | The algorithm should handle negative temperatures and zero correctly as numerical comparisons remain valid. |
| Extremely large input array | The chosen stack-based approach offers O(n) time complexity, making it efficient for large inputs. |
| Input array with mixed ascending and descending sequences | The monotonic stack correctly identifies the next warmer day by popping elements that are no longer candidates. |