Commit 321f684e by 李楚霏

work6 minimum_path_sum

parent 4b584bef
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
if(grid.size() == 0 || grid[0].size() == 0) return 0;
int row = grid.size();
int col = grid[0].size();
auto dp = vector< vector<int> > (row, vector<int> (col));
dp[0][0] = grid[0][0];
for (int i = 1 ; i < row; i++) {
dp[i][0] = dp[i-1][0] + grid[i][0];
}
for (int j = 1; j < col; j++) {
dp[0][j] = dp[0][j-1] + grid[0][j];
}
for(int i = 1; i < row; i++) {
for(int j = 1; j < col; j++) {
dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j];
}
}
return dp[row - 1][col - 1];
}
};
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment