Daily Temperatures - Leetcode 739

I'm a developer who loves to write. I started my blog because I wanted to share my knowledge and passion for technology with others. I write about a variety of topics, including coding, web development, and blockchain.
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 calledreswith 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
temperaturesslice 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
jis initialized toi + 1.jwill be used to traverse through thetemperaturesarray 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 thatjdoesn't go out of bounds.temperatures[j] <= temperatures[i]: Check if the temperature at dayjis 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 ifjis still within bounds, and if the temperature at dayjis indeed warmer than the temperature at dayi.If both conditions are met, it means a warmer temperature has been found, so the difference between
jandi(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
temperaturesarray have been processed, and theresslice is populated with the waiting days for each day.Finally, the function returns the
resslice, 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.




