leetcode-64. 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.

Example:

Input:
[
  [1,3,1],
  [1,5,1],
  [4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum.

给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。

说明:每次只能向下或者向右移动一步。

示例:

输入:
[
  [1,3,1],
  [1,5,1],
  [4,2,1]
]
输出: 7
解释: 因为路径 1→3→1→1→1 的总和最小。

思路:dp,用一个二维数组放 当前路径的最小值,res[i][j]=grid[i][j]+min(res[i-1][j],res[i][j-1])。当然也可以更简化只使用一维数组就可以了,就不写出来了。

class Solution {
public:
    int minPathSum(vector<vector<int>>& grid) {
        int m=grid.size(),n=grid[0].size(),i,j;
        if(!n || !m) return 0;
        int res[m][n];
        res[0][0]=grid[0][0];
        for(i=1;i<m;++i) res[i][0]=grid[i][0]+res[i-1][0];
        for(i=1;i<n;++i) res[0][i]=grid[0][i]+res[0][i-1];
        for(i=1;i<m;++i)
            for(j=1;j<n;++j)
            {
                res[i][j]=grid[i][j]+min(res[i-1][j],res[i][j-1]);
            }
        return res[m-1][n-1];  //注意这里返回m-1,n-1,不要使用i-1,j-1作为返回下标
    }
};

猜你喜欢

转载自blog.csdn.net/qq_21997625/article/details/86662387