哎呦,今天的每日一题之前已经做过了。正好给我节约时间准备明天的答辩😂,不过作为动态规划不是很熟练的同学,这道题归为简单题也有一点点难吧
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];
}
};