leet-code/daily-temperatures/sol.go

20 lines
430 B
Go
Raw Permalink Normal View History

2022-05-29 19:21:38 +00:00
package main
func dailyTemperatures(temperatures []int) []int {
out := make([]int, len(temperatures))
stack := make([][2]int, 0)
l := 0
for i := range temperatures {
if l != 0 && temperatures[i] > stack[l-1][1] {
for l != 0 && temperatures[i] > stack[l-1][1] {
out[stack[l-1][0]] = i - stack[l-1][0]
stack = stack[:l-1]
l--
}
}
stack = append(stack, [2]int{i, temperatures[i]})
l++
}
return out
}