added dp problems
parent
04d42af7c0
commit
75f0b66e63
|
@ -0,0 +1,37 @@
|
|||
// (problem Link) - [https://leetcode.com/problems/minimum-path-sum/]
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int minSumPathUtil(int i, int j,vector<vector<int>> &matrix,vector<vector<int>> &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<vector<int> > &matrix){
|
||||
vector<vector<int> > dp(n,vector<int>(m,-1));
|
||||
return minSumPathUtil(n-1,m-1,matrix,dp);
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
vector<vector<int> > matrix{{5,9,6},
|
||||
{11,5,2}};
|
||||
|
||||
int n = matrix.size();
|
||||
int m = matrix[0].size();
|
||||
|
||||
cout<<minSumPath(n,m,matrix);
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
// (problem link) - [https://leetcode.com/problems/unique-paths/]
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int countWaysUtil(int m, int n, vector<vector<int> > &dp) {
|
||||
for(int i=0; i<m ;i++){
|
||||
for(int j=0; j<n; j++){
|
||||
|
||||
//base condition
|
||||
if(i==0 && j==0){
|
||||
dp[i][j]=1;
|
||||
continue;
|
||||
}
|
||||
|
||||
int up=0;
|
||||
int left = 0;
|
||||
|
||||
if(i>0)
|
||||
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<vector<int> > dp(m,vector<int>(n,-1));
|
||||
return countWaysUtil(m,n,dp);
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
int m=3;
|
||||
int n=2;
|
||||
|
||||
cout<<countWays(m,n);
|
||||
}
|
Loading…
Reference in New Issue