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.
思路:依然是动态规划求解
代码:
int minPathSum(vector<vector<int> > &grid) {
int m=grid.size();
if(m == 0)
{
return 0;
}
int n=grid[0].size();
if(n == 0)
{
return 0;
}
int *map=new int[n];
map[0]=grid[0][0];
for(int i=1; i<n; ++i)
{
map[i]=grid[0][i]+map[i-1];;
}
for(int i=1; i<m; ++i)
{
for(int j=0; j<n; ++j)
{
if(j == 0)
{
map[0]+=grid[i][j];
}
else
{
map[j]=min(map[j-1],map[j])+grid[i][j];
}
}
}
return map[n-1];
}