1+ #https://leetcode.com/problems/climbing-stairs/
2+ class Solution (object ):
3+ #if there are 13 stairs
4+ #the max two steps are 6, so there can be
5+ #6 two step, 1 one step -> case1
6+ #5 two step, 3 one step -> case2
7+ # ...
8+ #0 two step, 13 one step -> case7
9+
10+ #each case may have more than one way to arrange
11+ #take case2 as example, 22222111. how many combination can it be?
12+ #you can think this as, there are 8 seats you have to choose 3 seats for number 1 to sit on it.
13+
14+ #we call this combination m choose n, thus
15+ #case1 is combination 7 choose 1
16+ #case2 is combination 8 choose 3
17+ # ...
18+ #case7 is combination 13 choose 13
19+ #the calculation of combination is m!/(n!*(m-n)!)
20+ #https://en.wikipedia.org/wiki/Combination
21+
22+ #Efficaiency is O(N), N is the stairs count.
23+ #Space is O(1)
24+
25+ def climbStairs (self , n ):
26+ counter = 0
27+ max_two_step = n / 2
28+ for two_step in range (max_two_step + 1 ):
29+ one_step = n - two_step * 2
30+ combination = self .combination (two_step + one_step , one_step )
31+ counter += combination
32+ return counter
33+
34+
35+ #combination m choose n
36+ def combination (self , m , n ):
37+ def factorial (int_num ):
38+ if int_num < 0 : return None
39+ if int_num == 0 : return 1
40+ counter = 1
41+ while int_num >= 1 :
42+ counter *= int_num
43+ int_num -= 1
44+ return counter
45+
46+ return factorial (m )/ (factorial (n )* factorial (m - n ))
0 commit comments