-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path[LeetCode] 70. Climbing Stairs.py
94 lines (63 loc) · 1.31 KB
/
[LeetCode] 70. Climbing Stairs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
class Solution:
def climbStairs(self, n: int) -> int:
if n < 3:
return n
mem = [0] * (n+1)
mem[1] = 1
mem[2] = 2
for i in range(3, n+1):
mem[i] = mem[i-1] + mem[i-2]
return mem[n]
'''
1. Problem
- stair
- at once 1 step or 2
- how many distinct way ?
2. TC
tc1)
n : 1
res : 1
tc2)
n : 2
res : 2
tc3)
n : 3
res : 3
111
12
21
tc4)
n : 4
res : 4
1111
211
121
112
22
3. Brain STorming
- relations
tc4)
n : 4
res : 4
111+1
21+1
12+1
=> S(3) + 1
11+2
2+2
=> S(2)+2
S(N) = S(before(1 step)) + S(before(2 step))
mem[n] = mem[n-1] + mem[n-2]
- edge case
n = 1
mem[1] = 1
n = 2
mem[2] = 2
4. Summarize
mem = [0] * (n+1)
mem[1] = 1
mem[2] = 2
for i in range(3, n):
mem[n] = mem[n-1] + mem[n-2]
return mem[n]
'''