1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
package main
import "fmt"
func dailyTemperaturesDummy(temperatures []int) []int {
l := len(temperatures)
res := make([]int, l)
for i := 0; i < l; i++ {
for j := i + 1; j < l; j++ {
if temperatures[j] > temperatures[i] {
res[i] = j - i
break
}
}
}
return res
}
func dailyTemperatures(temperatures []int) []int {
l := len(temperatures)
ret := make([]int, l)
stack := make([]int, 0)
for i := 0; i < l; i++ {
for len(stack) > 0 && temperatures[stack[len(stack)-1]] < temperatures[i] {
ret[stack[len(stack)-1]] = (i - stack[len(stack)-1])
stack = stack[:len(stack)-1]
}
stack = append(stack, i)
}
return ret
}
func main() {
fmt.Println(dailyTemperatures([]int{73, 74, 75, 71, 69, 72, 76, 73}))
fmt.Println(dailyTemperatures([]int{30, 40, 50, 60}))
fmt.Println(dailyTemperatures([]int{30, 60, 90}))
fmt.Println(dailyTemperatures([]int{90, 60, 90}))
}
|