Daily Temperatures
MediumGiven an array of integers temperatures
representing daily temperatures, return an array answer
where answer[i]
is the number of days you have to wait until a warmer temperature. If there is no future day with a warmer temperature, set answer[i] = 0
.
Example:
Input: temperatures = [73,74,75,71,69,72,76,73] Output: [1,1,4,2,1,1,0,0] Explanation: - At index 0 (73°F), the next warmer day is at index 1 (74°F), so answer[0] = 1. - At index 1 (74°F), the next warmer day is at index 2 (75°F), so answer[1] = 1. - At index 2 (75°F), the next warmer day is at index 6 (76°F), so answer[2] = 4. - At index 3 (71°F), the next warmer day is at index 5 (72°F), so answer[3] = 2. - At index 4 (69°F), the next warmer day is at index 5 (72°F), so answer[4] = 1. - At index 5 (72°F), the next warmer day is at index 6 (76°F), so answer[5] = 1. - At index 6 (76°F), there is no warmer future day, so answer[6] = 0. - At index 7 (73°F), there is no warmer future day, so answer[7] = 0.
Test Cases
Test Cases
Input
73 74 75 71 69 72 76 73
Expected Output
1 1 4 2 1 1 0 0
Step 1
Step 2
Step 3
Step 4
Step 1: Identify the Pattern
Daily Temperatures
MediumGiven an array of integers temperatures
representing daily temperatures, return an array answer
where answer[i]
is the number of days you have to wait until a warmer temperature. If there is no future day with a warmer temperature, set answer[i] = 0
.
Example:
Input: temperatures = [73,74,75,71,69,72,76,73] Output: [1,1,4,2,1,1,0,0] Explanation: - At index 0 (73°F), the next warmer day is at index 1 (74°F), so answer[0] = 1. - At index 1 (74°F), the next warmer day is at index 2 (75°F), so answer[1] = 1. - At index 2 (75°F), the next warmer day is at index 6 (76°F), so answer[2] = 4. - At index 3 (71°F), the next warmer day is at index 5 (72°F), so answer[3] = 2. - At index 4 (69°F), the next warmer day is at index 5 (72°F), so answer[4] = 1. - At index 5 (72°F), the next warmer day is at index 6 (76°F), so answer[5] = 1. - At index 6 (76°F), there is no warmer future day, so answer[6] = 0. - At index 7 (73°F), there is no warmer future day, so answer[7] = 0.
Test Cases
Test Cases
Input
73 74 75 71 69 72 76 73
Expected Output
1 1 4 2 1 1 0 0
Step 1
Step 2
Step 3
Step 4
Step 1: Identify the Pattern