aboutsummaryrefslogtreecommitdiffstats
path: root/70-go/main.go
blob: 0613cfe8535e5158867b6d5c91641f8d0d5c4808 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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))
}