-
Notifications
You must be signed in to change notification settings - Fork 43
/
climbing-stairs.py
154 lines (131 loc) · 3.44 KB
/
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""
70. Climbing Stairs
Easy
You are climbing a staircase. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Example 1:
Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
Example 2:
Input: n = 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step
Constraints:
1 <= n <= 45
"""
# V0
# IDEA : RECURSION + MEMORIZATION
# https://leetcode.com/explore/learn/card/recursion-i/255/recursion-memoization/1662/
class Solution(object):
def climbStairs(self, n):
cache = {}
def help(n):
if n in cache:
return cache[n]
if n <= 2:
res = n
else:
res = help(n-2) + help(n-1)
cache[n] = res
return res
return help(n)
# V0'
class Solution:
# Time: O(2^n)
# Space: O(n)
def climbStairs(self, n):
if n == 1:
return 1
if n == 2:
return 2
return self.climbStairs(n - 1) + self.climbStairs(n - 2)
# V1
# https://blog.csdn.net/coder_orz/article/details/51506414
# n = 1,2,3,4,5,6,7,8,9... -> output : 1,2,3,5,8,13,21,34....
# -> output(n) = output(n-2) + output(n-1)
# IDEA : Fibonacci SERIES
class Solution:
def climbStairs(self, n):
prev, current = 0, 1
for i in range(n):
prev, current = current, prev+current
return current
# V1'
# https://blog.csdn.net/coder_orz/article/details/51506414
# IDEA : DP
# RECURSION WOULD BE TOO SLOW, SO USE DP SPEED UP THE PROCESS (DP CAN RECORD HISTORY)
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
if n == 0 or n == 1 or n == 2:
return n
steps = [1, 1]
for i in range(2, n+1):
steps.append(steps[i-1] + steps[i-2])
return steps[n]
# V2
class Solution:
"""
:type n: int
:rtype: int
"""
def climbStairs(self, n):
prev, current = 0, 1
for i in range(n):
prev, current = current, prev + current,
return current
# Time: O(2^n)
# Space: O(n)
def climbStairs1(self, n):
if n == 1:
return 1
if n == 2:
return 2
return self.climbStairs(n - 1) + self.climbStairs(n - 2)
# V3
# Time: O(logn)
# Space: O(1)
import itertools
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
def matrix_expo(A, K):
result = [[int(i==j) for j in range(len(A))] \
for i in range(len(A))]
while K:
if K % 2:
result = matrix_mult(result, A)
A = matrix_mult(A, A)
K /= 2
return result
def matrix_mult(A, B):
ZB = list(zip(*B))
return [[sum(a*b for a, b in zip(row, col)) \
for col in ZB] for row in A]
T = [[1, 1],
[1, 0]]
return matrix_expo(T, n)[0][0]
# Time: O(n)
# Space: O(1)
class Solution2(object):
"""
:type n: int
:rtype: int
"""
def climbStairs(self, n):
prev, current = 0, 1
for i in range(n):
prev, current = current, prev + current,
return current