From 8d890f85750fb9f1c49b4abd6f0b5d6760ee4da6 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Mon, 12 Dec 2022 07:00:18 +0330 Subject: 70-go --- 70-go/go.mod | 3 +++ 70-go/main.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 70-go/go.mod create mode 100644 70-go/main.go (limited to '70-go') 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)) +} -- cgit v1.2.3