From 199884b22b841d20b2ad0ee5c14d39c9a9bbbc95 Mon Sep 17 00:00:00 2001 From: sai charan Date: Sat, 1 Oct 2022 09:28:30 +0530 Subject: [PATCH 1/2] added matrix-chain-multiplication -2 approaches --- .../matrixChainMultiplication.cpp | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 C++/dynamic programming/matrixChainMultiplication.cpp diff --git a/C++/dynamic programming/matrixChainMultiplication.cpp b/C++/dynamic programming/matrixChainMultiplication.cpp new file mode 100644 index 000000000..810728fa6 --- /dev/null +++ b/C++/dynamic programming/matrixChainMultiplication.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; + +// recursive solution +int MCM(int *arr, int i, int j){ + if(i >= j) return 0; + + int res = INT_MAX; + for(int k=i; k= j) return 0; + + if(dp[i][j] != -1) return dp[i][j]; + + int res = INT_MAX; + for(int k=i; k Date: Sat, 1 Oct 2022 09:34:54 +0530 Subject: [PATCH 2/2] added eggdropping code -2approaches --- C++/dynamic programming/eggDropping.cpp | 41 +++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 C++/dynamic programming/eggDropping.cpp diff --git a/C++/dynamic programming/eggDropping.cpp b/C++/dynamic programming/eggDropping.cpp new file mode 100644 index 000000000..64886fa52 --- /dev/null +++ b/C++/dynamic programming/eggDropping.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; + +// recursive solution +int eggDrop(int e, int f){ + if(f==0 || f==1) return f; + if(e == 1) return f; + + int res = INT_MAX; + for(int k=1; k<=f; k++){ + int tempAns = 1 + max(eggDrop(e, f-k), eggDrop(e-1, k-1)); + res = min(res, tempAns); + } + + return res; +} + +// memoization solution +int dp[100][100]; +int eggDrop_M(int e, int f){ + if(f==0 || f==1) return f; + if(e == 1) return f; + + if(dp[e][f] != -1) return dp[e][f]; + + int res = INT_MAX; + for(int k=1; k<=f; k++){ + int tempAns = 1 + max(eggDrop(e, f-k), eggDrop(e-1, k-1)); + res = min(res, tempAns); + } + + return dp[e][f] = res; +} + +int main(){ + int egg = 2, floor = 10; + memset(dp, -1, sizeof(dp)); + cout<