-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01- Fibonacci.cpp
More file actions
31 lines (27 loc) · 834 Bytes
/
Copy path01- Fibonacci.cpp
File metadata and controls
31 lines (27 loc) · 834 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
// In The Name of Allah
// Fibonacci (Bottom-up DP)
//
// Problem: Compute the n-th Fibonacci number, F(0)=0, F(1)=1,
// F(n) = F(n-1) + F(n-2).
// Approach: Tabulate F(0)..F(n) iteratively. Compare against the naive
// recursion (O(2^n)) to see why memoization matters.
// Time: O(n)
// Space: O(n) for the table; can be reduced to O(1) (see
// General/Fibonacci Iterative.cpp).
#include <iostream>
#include <vector>
using namespace std;
long long fibonacci(int n) {
if (n < 2) return n;
vector<long long> fib(n + 1);
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i <= n; i++) fib[i] = fib[i - 1] + fib[i - 2];
return fib[n];
}
int main() {
for (int n : {0, 1, 5, 10, 20, 50}) {
cout << "F(" << n << ") = " << fibonacci(n) << endl;
}
return 0;
}