Minimum Path Sum

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.

简单的动态规划题。刚开始又写成路径和了……

class Solution {
public:
    int minPathSum(vector<vector<int> > &grid) {
        vector<int> p(grid.size(),0);
        p[0] = grid[0][0];
        for(int i = 1; i < grid.size();i++)
        p[i] = p[i-1] + grid[i][0];
        
        for(int i =1; i < grid[0].size();i++)
        {
            p[0] = p[0]+grid[0][i];
            for(int j = 1 ; j < grid.size();j++)
            {
                p[j] = min(p[j],p[j-1]) + grid[j][i];
            }
        }
        return p[grid.size()-1];
        
    }
};

  

原文地址:https://www.cnblogs.com/pengyu2003/p/3595803.html