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