https://leetcode.com/problems/house-robber/description/
void robn(vector<int>& nums, vector<int>& res, int n) {
if(n == 1) {
res[1] = max(nums[0],nums[1]);
return;
}
robn(nums, res, n-1);
if(res[n-2] == res[n-1]) res[n] = res[n-2] + nums[n];
else res[n] = max(res[n-2]+nums[n], res[n-1]);
}
int rob(vector<int>& nums) {
if(nums.size() == 0) return 0;
if(nums.size() == 1) return nums[0];
vector<int> res(nums.size(), 0);
res[0] = nums[0];
robn(nums, res, nums.size()-1);
return res.back();
}
No comments:
Post a Comment