Likou-- the sword refers to the maximum value of offer47. gifts

 Topic link: Jianzhi Offer 47. The maximum value of a gift - LeetCode

The following is a detailed answer to this question using the idea of ​​​​dynamic programming. I believe that all friends can understand and master this question.

Reference Code:

class Solution {
public:
    int maxValue(vector<vector<int>>& grid) {
        int m=grid.size();
        int n=grid[0].size();
        //多开一行,多开一列
        vector<vector<int>> dp(m+1,vector<int>(n+1));
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                //状态转移方程
                dp[i][j]=max(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 solutions to the above questions are helpful to you, please highlight the following carefully and pay attention to them. We will continue to update the classic questions of dynamic programming in the future. See you in the next issue! ! ! !

 

おすすめ

転載: blog.csdn.net/weixin_70056514/article/details/131485285