From 79caccf99298f01644c3adef329739c3c6dc2df5 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Sun, 18 Dec 2022 08:05:19 +0330 Subject: 739 --- 739/go.mod | 3 +++ 739/main.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 739/go.mod create mode 100644 739/main.go 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})) +} -- cgit v1.2.3