File tree Expand file tree Collapse file tree 1 file changed +12
-20
lines changed Expand file tree Collapse file tree 1 file changed +12
-20
lines changed Original file line number Diff line number Diff line change 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 ]
You can’t perform that action at this time.
0 commit comments