-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path509_fib.py
More file actions
31 lines (24 loc) · 733 Bytes
/
Copy path509_fib.py
File metadata and controls
31 lines (24 loc) · 733 Bytes
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
from functools import lru_cache
class Solution:
cache = {}
def fib_easy(self, n: int) -> int: # 34.35% 24.22%
if n == 0 or n == 1:
return n
return self.fib_easy(n - 1) + self.fib_easy(n - 2)
@lru_cache(maxsize=32)
def fib(self, n: int) -> int:
if n < 2:
return n
return self.fib(n - 1) + self.fib(n - 2)
def fib_cache(self, n: int) -> int:
if n in self.cache:
return self.cache[n]
if n == 0 or n == 1:
return n
else:
result = self.fib(n - 1) + self.fib(n - 2)
self.cache[n] = result
return result
if __name__ == "__main__":
res = Solution().fib_cache(4)
print(res)