70.Climbing Stairs
My code
1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
memo = {}
def climbStairs(self, n: int) -> int:
if n == 2:
return 2
elif n == 1:
return 1
if n not in Solution.memo:
Solution.memo[n] = Solution.climbStairs(self,n - 1) + Solution.climbStairs(self,n - 2)
return Solution.memo[n]
1
2
Runtime : 40ms (beats 61.37%)
Memory : 16.39MB (beats 27.66%)
Bottom - up
1
2
3
4
5
6
7
8
class Solution:
memo = {0: 1, 1: 1}
def climbStairs(self, n: int) -> int:
for i in range(2,n + 1):
Solution.memo[i] = Solution.memo[i - 1] + Solution.memo[i - 2]
return Solution.memo[n]
1
2
Runtime : 40ms (beats 61.37%)
Memory : 16.35MB (Beats 27.66%)