-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00_Fibonacci.cpp
59 lines (54 loc) · 1.17 KB
/
00_Fibonacci.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
58
59
// Fibonacci Sequence Using DP
#include <bits/stdc++.h>
using namespace std;
class Solution1
{
// Tabulation : Bottoms UP
// Time complexity : O(N);
// Space Complexity : O(N) + O(N) ;
public:
int fibonacci(int n)
{
vector<int> dp(n);
dp[0] = 0, dp[1] = 1;
for (int i = 2; i < n; ++i)
{
dp[i] = dp[i - 1] + dp[i - 2];
cout << dp[i]<< " " ;
}
cout << endl;
return dp[n-1];
}
};
class Solution
{
// Memoization approach : top Down
// Time complexity : O(N);
// Space Complexity : O(N) + O(N) ;
public:
int fibonacci(int n)
{
vector<int> dp(n, -1);
solve(n, dp);
return dp[n] = solve(n - 1, dp) + solve(n - 2, dp);
}
int solve(int n, vector<int> &dp)
{
if (n <= 1)
return n;
if (dp[n] != -1)
return dp[n];
return dp[n] = solve(n - 1, dp) + solve(n - 2, dp);
}
};
int main()
{
Solution1 obj1;
int n;
cout << "Enter n: ";
cin >> n;
cout << "Fibonnaci n is : " << obj1.fibonacci(n) << endl;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}