-
Notifications
You must be signed in to change notification settings - Fork 17
/
climbing-stairs.py
66 lines (46 loc) · 1.48 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
import unittest
def mem_input(func):
def wrapper(*args):
self = args[0]
cur_sum = args[2] if len(args) == 3 else 0
if self.mem is None:
self.mem = {}
if cur_sum in self.mem:
return self.mem[cur_sum]
else:
self.mem[cur_sum] = func(*args)
return self.mem[cur_sum]
return wrapper
class Solution:
mem = None
@mem_input
def climbStairs1(self, n, cur_sum=0):
if cur_sum >= n:
return 1 if cur_sum == n else 0
else:
return self.climbStairs(n, cur_sum + 1) + \
self.climbStairs(n, cur_sum + 2)
def climbStairs(self, n: int) -> int:
step, prev_step, prev_prev_step = 1, 1, 0
for _ in range(n):
step = prev_step + prev_prev_step
prev_step, prev_prev_step = step, prev_step
return step
class TestSolution(unittest.TestCase):
def test0(self):
self.sol = Solution()
self.assertEqual(self.sol.climbStairs(0), 1)
def test1(self):
self.sol = Solution()
self.assertEqual(self.sol.climbStairs(1), 1)
def test2(self):
self.sol = Solution()
self.assertEqual(self.sol.climbStairs(2), 2)
def test3(self):
self.sol = Solution()
self.assertEqual(self.sol.climbStairs(3), 3)
def test5(self):
self.sol = Solution()
self.assertEqual(self.sol.climbStairs(5), 8)
if __name__ == "__main__":
unittest.main()