Leek-- 64. Minimum path sum

Topic Link: 64. Minimum Path Sum - LeetCode 

The following is the process of solving this problem with the idea of ​​​​dynamic programming. I believe that all of you can understand and master this classic dynamic programming problem.

Reference Code:

class Solution {
public:
    int minPathSum(vector<vector<int>>& grid) {
        int m=grid.size();
        int n=grid[0].size();
        //根据以上图解分析可知应该把整个dp表初始化为正无穷大,并且多开一行,多开一列
        vector<vector<int>> dp(m+1,vector<int>(n+1,INT_MAX));
        //初始化dp[0][1]和dp[1][0]两个特殊位置
        dp[0][1]=dp[1][0]=0;
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                dp[i][j]=min(dp[i-1][j],dp[i][j-1])+grid[i-1][j-1];
            }
        }
        return dp[m][n];
    }
};

The above is the whole process of analyzing this dp question, have you learned it yet? If the above solutions are helpful to you, then please be careful and pay attention to it. We will continue to update the classic questions of dynamic programming in the future. See you in the next issue! ! ! !

Guess you like

Origin blog.csdn.net/weixin_70056514/article/details/131492654