Skip to content

Commit 445751a

Browse files
committed
climbing stairs
1 parent 53ab86a commit 445751a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

climbing-stairs/bskkimm.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def __init__(self):
3+
self.memo = {}
4+
5+
def climbStairs(self, n: int) -> int:
6+
7+
# n = 3 --> 3: 111, 2: 12, 21: --> 3
8+
# n = 4 --> 4: 1111, 3: 112, 121, 211, 2: 22 --> 5
9+
# n = 5 --< 5: 11111,4: 1112, 1121, 1211, 2111, 3: 221, 212, 122, --> 8
10+
# n = 6 --> 6: 111111, 5: 5, 4: 2211, 1122, 1221, 2112, 1212, 2121, 3: 1 --> 13
11+
12+
# a(n+2) = a(n+1) + a(n)
13+
14+
if n == 1:
15+
return 1
16+
elif n == 2:
17+
return 2
18+
19+
if n in self.memo: # need to return when memo is available w.r.t n
20+
return self.memo[n]
21+
22+
self.memo[n] = self.climbStairs(n-1) + self.climbStairs(n-2)
23+
return self.memo[n]

0 commit comments

Comments
 (0)