diff options
author | terminaldweller <thabogre@gmail.com> | 2022-12-12 03:30:18 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2022-12-12 03:30:18 +0000 |
commit | 8d890f85750fb9f1c49b4abd6f0b5d6760ee4da6 (patch) | |
tree | f6b0b92130f4d8d489a42abb8a6e7781931e9202 /70 | |
parent | 28 (diff) | |
download | leetcode-8d890f85750fb9f1c49b4abd6f0b5d6760ee4da6.tar.gz leetcode-8d890f85750fb9f1c49b4abd6f0b5d6760ee4da6.zip |
70-go
Diffstat (limited to '')
-rw-r--r-- | 70-go/go.mod | 3 | ||||
-rw-r--r-- | 70-go/main.go | 30 |
2 files changed, 33 insertions, 0 deletions
diff --git a/70-go/go.mod b/70-go/go.mod new file mode 100644 index 0000000..a3df715 --- /dev/null +++ b/70-go/go.mod @@ -0,0 +1,3 @@ +module 70 + +go 1.19 diff --git a/70-go/main.go b/70-go/main.go new file mode 100644 index 0000000..0613cfe --- /dev/null +++ b/70-go/main.go @@ -0,0 +1,30 @@ +package main + +import "fmt" + +func climbStairs(n int) int { + var fact func(n int, memo map[int]int) int + + memo := make(map[int]int, n) + fact = func(n int, memo map[int]int) int { + if _, ok := memo[n]; ok { + return memo[n] + } + if n < 2 { + return 1 + } + + memo[n] = fact(n-1, memo) + fact(n-2, memo) + return memo[n] + } + + return fact(n, memo) +} + +func main() { + fmt.Println(climbStairs(2)) + fmt.Println(climbStairs(3)) + fmt.Println(climbStairs(4)) + fmt.Println(climbStairs(5)) + fmt.Println(climbStairs(44)) +} |