class Solution {
public:
int rob(vector<int>& nums) {
if(0 == nums.size())
return 0;
if(1 == nums.size())
return nums[0];
if(2 == nums.size())
return max(nums[0], nums[1]);
vector<int> max_chosen (nums.size(), 0x80000000);
vector<int> max_unchosen (nums.size(), 0x80000000);
max_chosen[0] = nums[0]; max_unchosen[0] = 0;
max_chosen[1] = nums[1]; max_unchosen[1] = max_chosen[0];
for(int i = 2; i < nums.size(); ++i){
max_chosen[i] = nums[i] +max(max_chosen[i-2], max_unchosen[i-2]);
max_unchosen[i] = max(max_chosen[i-1], max_unchosen[i-1]);
}
return max(max_chosen.back(), max_unchosen.back());
}
};
198. House Robber
最新推荐文章于 2022-03-08 22:40:25 发布