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.
int minPathSum(vector<vector<int> > &grid) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int row = grid.size();
int col = grid[0].size(); int**dp = new int*[row];
for(int i=0; i< row; i++)
dp[i] = new int[col]; dp[0][0] = grid[0][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 =0; j < col; j++)
if(j == 0)dp[i][0] = dp[i-1][0] + grid[i][0];
else{
int tmp = dp[i-1][j] < dp[i][j-1] ? dp[i-1][j] : dp[i][j-1];
dp[i][j] = tmp + grid[i][j];
} int tmp = dp[row-1][col-1];
for(int i = 0; i < row; i++)
delete[] dp[i];
delete[] dp;
return tmp;
}