1. class Solution {
    2. public:
    3. int minPathSum(vector<vector<int>>& grid) {
    4. int m = grid.size();
    5. int n = grid[0].size();
    6. vector<vector<int> > dp(m+1, vector<int>(n+1, 0));
    7. dp[0][0] = grid[0][0];
    8. for(int i = 1; i< m; i++){
    9. dp[i][0] = dp[i-1][0] + grid[i][0];
    10. }
    11. for(int j = 1; j< n; j++){
    12. dp[0][j] = dp[0][j-1] + grid[0][j];
    13. }
    14. for(int i = 1; i<m; i++){
    15. for(int j = 1; j<n; j++){
    16. dp[i][j] = min(dp[i][j-1], dp[i-1][j]) + grid[i][j];
    17. }
    18. }
    19. return dp[m-1][n-1];
    20. }
    21. };