Since you can only move right or down, the previous step to a point can only be from left or above. Set dp[i][j] to represent the minimum path length to reach grid[i][j]. We have:
classSolution { public: intminPathSum(vector<vector<int> > &grid) { int i, j, m = grid.size(), n = grid[0].size(); // m == grid.length // n == grid[i].length // 1 <= m, n <= 200 // 0 <= grid[i][j] <= 200
// You can only move down or right one step at a time. vector<vector<int> > dp(m, vector<int>(n, 0));
dp[0][0] = grid[0][0]; for (i = 1; i < m; i++) dp[i][0] = dp[i - 1][0] + grid[i][0];