您的位置:首页 > 职场人生

《慕课网玩转算法面试》笔记及习题解答9.4

2017-09-22 16:08 555 查看
198. House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it
will automatically contact the police if two adjacent houses were broken into on the same night
.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

思路:F(n) = max( F(n-1), nums
+ F(n-2) ),我觉得老师讲的方法还是太绕了,不如这个直观

class Solution {
public:
int rob(vector<int>& nums) {
vector<int> money(nums.size(), -1);
if(nums.empty()) return 0;
if(nums.size() < 2) return nums[0];
money[0] = nums[0];
money[1] = max(nums[0],nums[1]);
for(int i = 2; i < nums.size(); i++){
money[i] = max( money[i-1], nums[i] + money[i-2]);
}
return money.back();
}
};213. House Robber II
Note: This is an extension of House Robber.

After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.思路:这个题目相对于上面那道题主要难度在于不能同时抢劫队首和队尾, 我们可以强两次,第一次访问 a[0,...,n-1], 第二次访问 a[1,...,n-2],然后取两者之间的最大值
class Solution {
private:
int robHelper(vector<int>& nums, int start, int end){
if(start == end)
return nums[start];
vector<int> money(nums.size(), -1);
money[start] = nums[start];
money[start+1] = max(nums[start],nums[start+1]);
for(int i = start+2; i<= end; i++)
{
if(money[i] != -1)
continue;
money[i] = max(money[i-1], nums[i] + money[i-2]);
}

return money[end];
}
public:
int rob(vector<int>& nums) {
if(nums.empty()) return 0;
if(nums.size() < 2) return nums[0];

return max( robHelper(nums, 0, nums.size()-2), robHelper(nums, 1, nums.size()-1) );
}
};
337. House Robber III

The thief has found himself a new place for his thievery again. There is only one entrance to this ar
4000
ea, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in
this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.

Determine the maximum amount of money the thief can rob tonight without alerting the police.

思路:其实差不多,主要要注意状态的转移,需要传两个变量来保存左右子树的状态量
class Solution {
private:
int treeRob(const TreeNode*root, int& l, int& r){
if(root == NULL)
return 0;
int ll = 0, lr = 0, rl = 0, rr = 0 ;
l = treeRob(root->left, ll, lr);
r = treeRob(root->right, rl, rr);
return max( l + r, root->val + ll + lr + rl+ rr );
}

public:
int rob(TreeNode* root) {
int l,r;
return treeRob(root, l,r);
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: