Skip to content

Commit 0a8e5ba

Browse files
committed
Week 2: Chage the code
1 parent 663d827 commit 0a8e5ba

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

climbing-stairs/mandel-17.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
1-
class Solution:
2-
def climbStairs(self, n: int) -> int:
3-
def factorial(n):
4-
result = 1
5-
for i in range(1, n+1):
6-
result *= i
7-
return result
8-
9-
def combination(n, r):
10-
top = factorial(n)
11-
bottom = factorial(n-r) * factorial(r)
12-
return top / bottom
1+
import collections
132

14-
share = n // 2
15-
total = 1
16-
for i in range(1, share + 1):
17-
combination_n = n - i
18-
combination_r = i
19-
20-
total += combination(combination_n, combination_r)
3+
class Solution:
4+
num_dict = collections.defaultdict(int)
215

22-
return int(total)
6+
def climbStairs(self, n: int) -> int:
7+
if n <= 2:
8+
return n
9+
10+
if self.num_dict[n]:
11+
return self.num_dict[n]
12+
13+
self.num_dict[n] = self.climbStairs(n-1) + self.climbStairs(n-2)
14+
return self.num_dict[n]

0 commit comments

Comments
 (0)