-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNthFibonacci.py
44 lines (33 loc) · 897 Bytes
/
NthFibonacci.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
#Approach 1: Recursive
def getNthFibRec(n):
if n==2:
return 1
elif n==1:
return 0
else:
return getNthFib(n-1) + getNthFib(n-2)
# Time Complexity = O(2^n)
# Space Complexity = O(n)
#############################
#Approach 2: Memoization
def getNthFibMem(n, memoize={1:0, 2:1}):
if n in memoize:
return memoize[n]
else:
memoize[n] = getNthFibMem(n-1, memoize) + getNthFibMem(n-2, memoize)
return memoize[n]
# Time Complexity = O(n)
# Space Complexity = O(n)
#############################
#Approach 3: Iterative
def getNthFibIter(n):
lastTwo = [0,1]
counter = 3
while counter<=n:
nextFib = lastTwo[0] + lastTwo[1]
lastTwo[0] = lastTwo[1]
lastTwo[1] = nextFib
counter+=1
return lastTwo[1] if n>1 else lastTwo[0]
# Time Complexity = O(n)
# Space Complexity = O(1)