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.
Accept: 8ms, 动态规划
int dp[1000][1000];
int minPathSum(int **grid, int nRows, int nCols) {
for (int i = nRows - 1; i >= 0; --i) {
for (int j = nCols - 1; j >= 0; --j) {
int min = INT_MAX;
if (i+1 < nRows) {
min = dp[i+1][j];
}
if (j+1 < nCols) {
if (dp[i][j+1] < min) {
min = dp[i][j+1];
}
}
if (min < INT_MAX) {
dp[i][j] = grid[i][j] + min;
} else {
dp[i][j] = grid[i][j];
}
}
}
return dp[0][0];
}