class Solution {
public:
int rob(vector<int>& nums) {
if(!nums.size())
return 0;
vector<int> v(nums.size());
for(int i = 0; i < v.size(); ++i){
if(i == 0)
v[0] = nums[0];
else if(i == 1)
v[1] = max(nums[0], nums[1]);
else
v[i] = max(v[i - 2] + nums[i], v[i-1]);
}
int max = INT_MIN;
for(auto x: v) {
if(x > max)
max = x;
}
return max;
}
};
打家劫舍
最新推荐文章于 2024-06-24 22:12:53 发布