-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNthFibonacci.py
More file actions
32 lines (29 loc) · 1012 Bytes
/
NthFibonacci.py
File metadata and controls
32 lines (29 loc) · 1012 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
# Naive approach that takes up a lot of space, and is very slow
# O(2^N) time | O(N) space
def getNthFibNaive(n):
if n == 2:
return 1
elif n == 1:
return 0
else:
return getNthFibNaive(n - 1) + getNthFibNaive(n - 2)
# same as the naive approach but reduces the time complexity
# by avoiding duplicate calculations, uses the same amount of space
# O(N) time | O(N) space
def getNthFibCache(n, cache = {1: 0, 2: 1}):
if n in cache:
return cache[n]
else:
n = getNthFibCache(n - 1, cache) + getNthFibCache(n - 2, cache)
return cache[n]
# reduces space complexity significantly, as we are only storing 2 values in an array
# and manipulating the same array
# O(N) time | O(1) space
def getNthFibIter(n):
lastTwo = [0, 1]
counter = 3
while counter <= n:
nextFib = lastTwo[0] + lastTwo[1]
lastTwo[0] = lastTwo[1]
lastTwo[1] = nextFib
return lastTwo[1] if n > 1 else lastTwo[0]