Last active
September 18, 2019 15:58
-
-
Save lbvf50mobile/4b64e39751406ec5d1af3dcd5d3d5d14 to your computer and use it in GitHub Desktop.
climbStairs
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
# Runtime: 32 ms, faster than 68.51% of Ruby online submissions for Climbing Stairs. | |
# Memory Usage: 9.3 MB, less than 100.00% of Ruby online submissions for Climbing Stairs. | |
# https://leetcode.com/problems/climbing-stairs/submissions/ | |
def cs(i,n,mem) | |
return 0 if i > n | |
return 1 if i == n | |
return mem[i] if mem[i] | |
mem[i] = cs(i+1, n, mem) + cs(i+2,n,mem) | |
end | |
def climb_stairs(n) | |
mem = {} | |
cs(0,n,mem) | |
end |
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
// https://leetcode.com/problems/climbing-stairs/ | |
// Time limit exceed | |
func climbStairs(n int) int { | |
counter := 0 | |
// https://gobyexample.com/closures | |
// https://github.com/golang/go/issues/226 | |
var recursion func(input int) | |
recursion = func (input int){ | |
if 0 == input { | |
counter += 1 | |
} | |
if 0 > input { | |
return | |
} | |
recursion(input - 1) | |
recursion(input - 2) | |
} | |
recursion(n) | |
return counter | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment