diff options
author | terminaldweller <thabogre@gmail.com> | 2022-12-18 04:35:19 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2022-12-18 04:35:19 +0000 |
commit | 79caccf99298f01644c3adef329739c3c6dc2df5 (patch) | |
tree | 499f7b6879eb5df4add76a81478ff712a0a06a9e /7 | |
parent | 150 (diff) | |
download | leetcode-79caccf99298f01644c3adef329739c3c6dc2df5.tar.gz leetcode-79caccf99298f01644c3adef329739c3c6dc2df5.zip |
739
Diffstat (limited to '')
-rw-r--r-- | 739/go.mod | 3 | ||||
-rw-r--r-- | 739/main.go | 42 |
2 files changed, 45 insertions, 0 deletions
diff --git a/739/go.mod b/739/go.mod new file mode 100644 index 0000000..4f9d173 --- /dev/null +++ b/739/go.mod @@ -0,0 +1,3 @@ +module 739 + +go 1.19 diff --git a/739/main.go b/739/main.go new file mode 100644 index 0000000..2870e41 --- /dev/null +++ b/739/main.go @@ -0,0 +1,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})) +} |