Table of contents
Problem - Leetcode
Given an array of integers temperatures
represent the daily temperatures, return an arrayanswer
such that answer[i]
is the number of days you have to wait after the i<sup>th</sup>
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 <= 10<sup>5</sup>
30 <= temperatures[i] <= 100
Solution in Golang
func dailyTemperatures(temperatures []int) []int {
res := make([]int, len(temperatures))
for i := len(temperatures) - 1; i >= 0; i-- {
j := i + 1
for j < len(temperatures) && temperatures[j] <= temperatures[i] {
if res[j] <= 0 {
break
}
j += res[j]
}
if j < len(temperatures) && temperatures[j] > temperatures[i] {
res[i] = j - i
}
}
return res
}
This code defines a Go function called dailyTemperatures
that takes a slice of integers called temperatures
as input and returns another slice of integers as output. The purpose of this function is to calculate the number of days you have to wait until a warmer temperature is forecasted for each day in the input.
Here's a detailed explanation of how the code works:
res := make([]int, len(temperatures))
: This line initializes an integer slice calledres
with the same length as the inputtemperatures
. This slice will store the number of days to wait for a warmer temperature for each day in the input.The code then enters a loop that iterates through the
temperatures
slice in reverse order, starting from the last day and moving towards the first day:for i := len(temperatures) - 1; i >= 0; i--
.Inside this loop, a variable
j
is initialized toi + 1
.j
will be used to traverse through thetemperatures
array looking for a warmer temperature.Another inner loop is used to find the next warmer temperature. It continues as long as two conditions are met:
j < len(temperatures)
: Ensure thatj
doesn't go out of bounds.temperatures[j] <= temperatures[i]
: Check if the temperature at dayj
is less than or equal to the temperature at dayi
.
Within the inner loop, there's a conditional check:
if res[j] <= 0
. This checks if there's already a result for dayj
. Ifres[j]
is less than or equal to 0, it means we haven't found a warmer temperature yet, so we break out of the inner loop.If the inner loop is exited without finding a warmer temperature, the code proceeds to the next step.
After exiting the inner loop, there's another conditional check:
if j < len(temperatures) && temperatures[j] > temperatures[i]
. This checks ifj
is still within bounds, and if the temperature at dayj
is indeed warmer than the temperature at dayi
.If both conditions are met, it means a warmer temperature has been found, so the difference between
j
andi
(i.e.,j - i
) is assigned tores[i]
. This indicates how many days you have to wait until a warmer temperature is forecasted for dayi
.The outer loop continues until all days in the
temperatures
array have been processed, and theres
slice is populated with the waiting days for each day.Finally, the function returns the
res
slice, which contains the number of days to wait for a warmer temperature for each day in the input.
In summary, this code efficiently calculates the number of days you have to wait for a warmer temperature for each day, taking into account the temperature forecasts for the upcoming days. It uses a nested loop and dynamic programming to optimize the process.