-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathnth-fib.cpp
58 lines (40 loc) · 921 Bytes
/
nth-fib.cpp
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
// Sol 1 Recursive -
// T.C - O(2^N)
// S.C - O(N)
using namespace std;
int getNthFib(int n) {
if(n == 1) return 0;
if(n == 2) return 1;
return getNthFib(n-1) + getNthFib(n-2);
}
// Sol 2 Memoization - Avoiding Duplicates:
// T.C - O(N)
// S.C - O(N)
#include <unordered_map>
using namespace std;
int getNthFib(int n) {
unordered_map<int, int> memoize({{1, 0}, {2, 1}});
return helper(n, memoize);
}
int helper(int n, unordered_map<int, int> &memoize) {
if (memoize.find(n) != memoize.end()) {
return memoize[n];
} else {
memoize[n] = helper(n - 1, memoize) + helper(n - 2, memoize);
return memoize[n];
}
}
// Sol - 3 Iterative Best Among All
// T.C - O(N)
// S.C - O(1)
using namespace std;
int getNthFib(int n) {
int arr[] = {0,1};
int next;
for(int i=3; i<=n; i++) {
next = arr[0] + arr[1];
arr[0] = arr[1];
arr[1] = next;
}
return n>1 ? arr[1] : arr[0];
}