Skip to content

Instantly share code, notes, and snippets.

@azihsoyn
Last active June 23, 2021 08:23
Show Gist options
  • Save azihsoyn/9e82826440ee3388f5c1dfb4acf43db9 to your computer and use it in GitHub Desktop.
Save azihsoyn/9e82826440ee3388f5c1dfb4acf43db9 to your computer and use it in GitHub Desktop.
var memo map[int]int
func init(){
memo = make(map[int]int)
memo[1] = 1
memo[2] = 2
}
func climbStairs(n int) int {
if v, ok := memo[n]; ok {
return v
}
memo[n] = climbStairs(n-1) + climbStairs(n-2)
return memo[n]
}
func climbStairs(n int) int {
if n == 1 {
return 1
} else if n == 2 {
return 2
}
memo := make(map[int]int, n)
memo[1] = 1
memo[2] = 2
return solve(n, memo)
}
func solve(n int, memo map[int]int) int {
if v, ok := memo[n]; ok {
return v
}
memo[n] = solve(n-1, memo) + solve(n-2, memo)
return memo[n]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment