leetcode五月每日一题 leetcode 198

在这里插入图片描述
哎呦,今天的每日一题之前已经做过了。正好给我节约时间准备明天的答辩😂,不过作为动态规划不是很熟练的同学,这道题归为简单题也有一点点难吧

class Solution {


public:
    int rob(vector<int>& nums) {


       int n = nums.size();
       if( n == 0 ) return 0;

       vector<int> memo(n ,-1);
       memo[n-1] = nums[n-1];

        for( int i = n - 2; i >= 0; i-- )
            //memo[i]
            for( int j = i ; j < n ; j ++ )
                memo[i] = max( memo[i], nums[j] + (j+2 < n ? memo[j+2] : 0 ));

                return memo[0];
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章