forked from SoumyadeepMukherjee/Matrix_Manipulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix_Manupulation.cpp
39 lines (35 loc) · 990 Bytes
/
Matrix_Manupulation.cpp
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
32
33
34
35
36
37
38
39
class Solution {
private:
vector<int> arr = {0,1,0};
public:
int minPathSum(vector<vector<int>>& grid) {
vector<vector<int>> dp( grid.size() , vector<int> (grid[0].size(), -1));
dp[0][0] = dfs(grid,0,0,dp);
return dp[0][0];
}
int dfs(vector<vector<int>>& grid, int i,int j,vector<vector<int>>& dp )
{
if(dp[i][j] != -1)
return dp[i][j];
int x,y,result = 10000;
for(int d=0;d<=1;d++)
{
x = i + arr[d];
y = j + arr[d+1];
if(x >= 0 && y >= 0 && x < grid.size() && y < grid[x].size())
{
result = min(result,dfs(grid,x,y,dp));
}
}
if(result == 10000)
{
dp[i][j] = grid[i][j];
return dp[i][j];
}
else
{
dp[i][j] = grid[i][j] + result;
return dp[i][j];
}
}
};