-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMinimum_Path_Sum.cpp
More file actions
34 lines (29 loc) · 830 Bytes
/
Minimum_Path_Sum.cpp
File metadata and controls
34 lines (29 loc) · 830 Bytes
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
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int n=grid.size();
int m=grid[0].size();
vector<vector<int>> dp(n,vector<int>(m,0));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(i==0 && j==0){
dp[i][j]+=grid[i][j];
}
else{
int dw=grid[i][j];
if(i>0) dw+=dp[i-1][j];
else{
dw+=1e9;
}
int rt=grid[i][j];
if(j>0) rt+=dp[i][j-1];
else{
rt+=1e9;
}
dp[i][j]=min(dw,rt);
}
}
}
return dp[n-1][m-1];
}
};