AC 10ms
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.
和前面的两题差不多
不过也就是约束条件变了
dp[i][j] =min( dp[i-1][j] + dp[i][j-1]) + grid[i][j];
int minPathSum(int** grid, int gridRowSize, int gridColSize) {
int m = gridRowSize;
int n = gridColSize;
int **dp=malloc(sizeof(int*)*m);
for(int i=0;i<m;i++)
{
dp[i] = malloc(sizeof(int)*n);
}
int premin=0;
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
if(i==0&&j==0)
{
dp[0][0]=grid[0][0];
continue;
}
else if(i==0||j==0)
{
if(i==0)
{
dp[i][j]=dp[i][j-1]+grid[i][j];
}
if(j==0)
{
dp[i][j]=dp[i-1][j]+grid[i][j];
}
}
else
{ premin = dp[i-1][j]>dp[i][j-1]?dp[i][j-1]:dp[i-1][j];
dp[i][j]=premin+grid[i][j];
}
}
}
return dp[m-1][n-1];
}