From 4465288f60cc8fea1caf62f7d2ad1c17aee6bd16 Mon Sep 17 00:00:00 2001 From: pkdism Date: Fri, 25 Nov 2016 18:36:34 +0530 Subject: [PATCH] Add DP implementations --- Dynamic Programming/Fibonacci_Bottom_Up.cpp | 19 ++++++++++++++++ Dynamic Programming/Fibonacci_Top_Down.cpp | 24 +++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 Dynamic Programming/Fibonacci_Bottom_Up.cpp create mode 100644 Dynamic Programming/Fibonacci_Top_Down.cpp diff --git a/Dynamic Programming/Fibonacci_Bottom_Up.cpp b/Dynamic Programming/Fibonacci_Bottom_Up.cpp new file mode 100644 index 000000000..cbd0912e8 --- /dev/null +++ b/Dynamic Programming/Fibonacci_Bottom_Up.cpp @@ -0,0 +1,19 @@ +#include +using namespace std; +int fib(int n){ + int res[n+1]; + res[0] = 0; res[1] = 1; + for(int i=2;i<=n;i++){ + res[i] = res[i-1] + res[i-2]; + } + return res[n]; +} +int main(int argc, char const *argv[]) +{ + int n; + cout<<"Enter n: "; + cin>>n; + cout<<"Fibonacci number is "; + cout< +using namespace std; +int arr[1000000]; +int fib(int n){ + if(arr[n]==-1){ + if(n<=1) + arr[n] = n; + else + arr[n] = fib(n-1) + fib(n-2); + } + return arr[n]; +} +int main(int argc, char const *argv[]) +{ + int n; + cout<<"Enter n: "; + cin>>n; + for (int i = 0; i < n+1; ++i) + { + arr[i] = -1; + } + cout<<"Fibonacci number is "<