From 75f0b66e639a3793a0c925e8ef8c7bd534d18cc1 Mon Sep 17 00:00:00 2001 From: Santanu Jana Date: Sun, 9 Oct 2022 12:19:35 +0530 Subject: [PATCH] added dp problems --- .../Dynamic-Programming/minimum-pathsum.cpp | 37 +++++++++++++++ .../Dynamic-Programming/unique-paths.cpp | 46 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 algorithms/CPlusPlus/Dynamic-Programming/minimum-pathsum.cpp create mode 100644 algorithms/CPlusPlus/Dynamic-Programming/unique-paths.cpp diff --git a/algorithms/CPlusPlus/Dynamic-Programming/minimum-pathsum.cpp b/algorithms/CPlusPlus/Dynamic-Programming/minimum-pathsum.cpp new file mode 100644 index 00000000..ece3d2cc --- /dev/null +++ b/algorithms/CPlusPlus/Dynamic-Programming/minimum-pathsum.cpp @@ -0,0 +1,37 @@ +// (problem Link) - [https://leetcode.com/problems/minimum-path-sum/] + +#include + +using namespace std; + +int minSumPathUtil(int i, int j,vector> &matrix,vector> &dp) +{ + if(i==0 && j == 0) + return matrix[0][0]; + if(i<0 || j<0) + return 1e9; + if(dp[i][j]!=-1) return dp[i][j]; + + int up = matrix[i][j]+minSumPathUtil(i-1,j,matrix,dp); + int left = matrix[i][j]+minSumPathUtil(i,j-1,matrix,dp); + + return dp[i][j] = min(up,left); + +} + +int minSumPath(int n, int m, vector > &matrix){ + vector > dp(n,vector(m,-1)); + return minSumPathUtil(n-1,m-1,matrix,dp); + +} + +int main() { + + vector > matrix{{5,9,6}, + {11,5,2}}; + + int n = matrix.size(); + int m = matrix[0].size(); + + cout< + +using namespace std; + +int countWaysUtil(int m, int n, vector > &dp) { + for(int i=0; i0) + up = dp[i-1][j]; + if(j>0) + left = dp[i][j-1]; + + dp[i][j] = up+left; + } + } + + return dp[m-1][n-1]; + + +} + +int countWays(int m, int n){ + vector > dp(m,vector(n,-1)); + return countWaysUtil(m,n,dp); + +} + +int main() { + + int m=3; + int n=2; + + cout<