-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLeetCode#51.cc
35 lines (31 loc) · 997 Bytes
/
LeetCode#51.cc
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
class Solution {
private:
void dfs(int x, int y,vector<vector<int> >& grid, vector<vector<int> >& dp){
if(dp[x][y]!=-1) return ;
if(x>0){
dfs(x-1,y,grid,dp);
if(dp[x][y]==-1 || dp[x][y]>grid[x][y]+dp[x-1][y])
dp[x][y]=dp[x-1][y]+grid[x][y];
}
if(y>0){
dfs(x,y-1,grid,dp);
if(dp[x][y]==-1 || dp[x][y]>grid[x][y]+dp[x][y-1])
dp[x][y]=dp[x][y-1]+grid[x][y];
}
return ;
}
public:
int minPathSum(vector<vector<int> > &grid) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int N = grid.size();
if(N==0) return 0;
int M = grid[0].size();
if(M==0) return 0;
vector<vector<int> > dp;
for(int i=0;i<N;i++) dp.push_back(vector<int>(M,-1));
dp[0][0]=grid[0][0];
dfs(N-1,M-1,grid,dp);
return dp[N-1][M-1];
}
};