Skip to content

Commit 0197303

Browse files
authored
Merge pull request kelvins#271 from metacoder87/patch-7
Create FibonacciMemoization.cpp
2 parents 3e965e7 + bb5377a commit 0197303

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/cpp/FibonacciMemoization.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
vector<int> memo;
7+
8+
int fibonacciMemoization(int n) {
9+
if (n <= 1) {
10+
return n;
11+
}
12+
if (memo[n] != -1) {
13+
return memo[n];
14+
}
15+
16+
memo[n] = fibonacciMemoization(n - 1) + fibonacciMemoization(n - 2);
17+
return memo[n];
18+
}
19+
20+
int main() {
21+
int test_nbr = 12;
22+
23+
// Initialize the memoization table with -1 (uncomputed)
24+
memo.assign(test_nbr + 1, -1);
25+
26+
cout << "memoization: " << fibonacciMemoization(test_nbr) << endl;
27+
return 0;
28+
}

0 commit comments

Comments
 (0)