Skip to content

Commit e77a32a

Browse files
committed
Python solution for Climbing stairs DP - Memoization and bottom up approach
1 parent 017b7a3 commit e77a32a

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# https://leetcode.com/problems/climbing-stairs/submissions/
2+
3+
# Bottom Up approach
4+
5+
class Solution:
6+
def climbStairs(self, n: int) -> int:
7+
if(n == 1):
8+
return 1
9+
dp = [0]*(n)
10+
dp[0] = 1
11+
dp[1] = 2
12+
for i in range(2 , n):
13+
dp[i] = dp[i-1] + dp[i-2]
14+
return dp[n-1]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# https://leetcode.com/problems/climbing-stairs/
2+
3+
# Memoization
4+
5+
from functools import lru_cache
6+
7+
class Solution:
8+
@lru_cache(maxsize = 1000)
9+
def climbStairs(self, n: int) -> int:
10+
if(n == 0 or n == 1):
11+
return 1
12+
else:
13+
return self.climbStairs(n-1) + self.climbStairs(n-2)

0 commit comments

Comments
 (0)