Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
tips: 1 remember not every dynamic programming need d[m+1][n+1] like this one.
class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(grid.size()<=0) return 0;
int m = grid.size();
int n = grid[0].size();
int d[m][n];
d[0][0] = grid[0][0];
for(int i=1; i<m; i++) {
d[i][0] = d[i-1][0] + grid[i][0];
}
for(int j=1; j<n; j++){
d[0][j] = d[0][j-1] + grid[0][j];
}
for( int i=1; i<m; i++) {
for( int j=1; j<n; j++) {
d[i][j] = grid[i][j] + min( d[i][j-1], d[i-1][j] ) ;
}
}
return d[m-1][n-1];
}
};