forked from atilsamancioglu/DA2-InterviewChallengeSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonaccinumber.py
More file actions
38 lines (29 loc) · 914 Bytes
/
Copy pathfibonaccinumber.py
File metadata and controls
38 lines (29 loc) · 914 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
32
33
34
35
36
37
38
'''
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,
'''
#Recursive
class Solution:
def fib(self, n: int) -> int:
if n == 0 or n == 1:
return n
else:
return self.fib(n-1) + self.fib(n-2)
#Iterative
class Solution:
def fib(self, n: int) -> int:
x,y = 0,1
for i in range(n):
x,y = y,x+y
return x
#Memoization - we should include a list of fib number calculations to see the effect
class Solution:
def fib(self, n: int) -> int:
def iterativeSolution(n):
x,y = 0,1
for i in range(n):
x,y = y,x+y
return x
memo = {}
if n not in memo:
memo[n] = iterativeSolution(n)
return memo[n]