Last active
June 23, 2021 08:23
-
-
Save azihsoyn/9e82826440ee3388f5c1dfb4acf43db9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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