-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfib.py
More file actions
executable file
·68 lines (56 loc) · 1.05 KB
/
fib.py
File metadata and controls
executable file
·68 lines (56 loc) · 1.05 KB
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
#!/usr/bin/python
# vim: foldlevel=0
"""
Implement a function which returns fib(N) where:
- fib(0) = 1
- fib(1) = 1
- and fib(N) = fib(N-1) + fib(N-2)
"""
def fib_recursive(N):
"""
Time complexity: O(2^n)
>>> fib_recursive(2)
2
>>> fib_recursive(4)
5
>>> fib_recursive(12)
233
"""
if N == 0 or N == 1:
return 1
return fib_recursive(N-1) + fib_recursive(N-2)
def fib_dp(N):
"""
Time complexity: O(n)
>>> fib_dp(2)
2
>>> fib_dp(4)
5
>>> fib_dp(12)
233
"""
memo = [0] * (N+1)
memo[0] = 1
memo[1] = 1
for i in range(2, N+1):
memo[i] = memo[i-1] + memo[i-2]
return memo[N]
memo = dict()
memo[0] = 1
memo[1] = 1
def fib_dp_top_down(N):
"""
Time complexity: O(n)
>>> fib_dp_top_down(2)
2
>>> fib_dp_top_down(4)
5
>>> fib_dp_top_down(12)
233
"""
if not memo.get(N):
memo[N] = fib_dp_top_down(N-1) + fib_dp_top_down(N-2)
return memo[N]
if __name__ == "__main__":
import doctest
doctest.testmod()