Likou-- 198. Robbery

Topic link: 198. Robbery - LeetCode 

Before reading this question, you can read the blog post of the masseuse, and the details are more detailed. (4 messages) Likou--Interview Question 17.16. Masseuse_Gobbi 0824's Blog-CSDN Blog

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 rob(vector<int>& nums) {
        int n=nums.size();
        vector<int> f(n);
        vector<int> g(n);
        f[0]=nums[0];
        //可写可不写,vector默认是0
        g[0]=0;
        for(int i=1;i<n;i++)
        {
            f[i]=g[i-1]+nums[i];
            g[i]=max(f[i-1],g[i-1]);
        }
        return max(f[n-1],g[n-1]);
    }
};

The above is the whole process of analyzing this topic with the idea of ​​dynamic programming, have you learned it? 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/131532931