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.
动态规划即可,与Unique Path类似
class Solution {
public:
int minPathSum(vector<vector<int> > &grid) { int m=grid.size();
int n=grid[].size(); /* int **dp=new int *[m];
for(int i=0;i<m;i++)
{
dp[i]=new int[n];
}
*/ vector<vector<int>> dp(m,vector<int>(n)); dp[][]=grid[][]; for(int i=;i<m;i++)
{
dp[i][]=dp[i-][]+grid[i][];
} for(int j=;j<n;j++)
{
dp[][j]=dp[][j-]+grid[][j];
} for(int i=;i<m;i++)
{
for(int j=;j<n;j++)
{
dp[i][j]=grid[i][j]+min(dp[i-][j],dp[i][j-]);
}
} return dp[m-][n-]; }
};