Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Dec 14, 2023
1 parent 52cd41f commit d95a456
Showing 1 changed file with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"fmt"
)

func dailyTemperatures(temperatures []int) []int {
result := make([]int, len(temperatures))
for i := 0; i < len(temperatures); i++ {
for j := i + 1; j < len(temperatures); j++ {
if temperatures[j] > temperatures[i] {
result[i] = j - i
break
}
}
}
return result
}

func dailyTemperatures1(temperatures []int) []int {
result := make([]int, len(temperatures))
stk := []int{}
for i, t := range temperatures {
for len(stk) > 0 && temperatures[stk[len(stk)-1]] < t {
j := stk[len(stk)-1]
result[j] = i - j
stk = stk[:len(stk)-1]
}
stk = append(stk, i)
}
return result
}

func main() {
temperatures := []int{73, 74, 75, 71, 69, 72, 76, 73}
fmt.Println(dailyTemperatures(temperatures))
}

0 comments on commit d95a456

Please sign in to comment.