思路:
Dp
代码:
class Solution {
public:
int rob(vector<int>& nums) {
int n = nums.size();
int res = 0;
vector<int> f(n+1);
f[0] = 0;
for(int i = 1;i <= n;i++){
f[i] = nums[i - 1];
//偷
if(i > 1) f[i] += f[i - 2];
//不偷
f[i] = max(f[i],f[i - 1]);
}
return f[n];
}
};
评论区