198. House Robber
已经有说明了,这篇第九个题。
http://blog.youkuaiyun.com/bestzem/article/details/51884230
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.
绝大部分继承自上一题,主要思路:由于前后数组相连,结果关系包含肯定是要么有头,要么有尾,要么两者都没有。此时我们可以计算一次在没有头的情况最大能得到,计算一次在没有尾的时候最大能得到多少,两次取最大即可。
class Solution {
public:
int rob(vector<int>& nums) {
int sz=nums.size();
if(nums.empty()) return 0;
if(sz==1) return nums[0];
if(sz==2) return max(nums[0],nums[1]);
vector<int> maxRob(sz,0);
maxRob[1]=nums[1];
maxRob[2]=nums[2];
for(int i=3;i<sz;++i)//get without nums[0]
{
maxRob[i]=nums[i]+*(max_element(maxRob.cbegin()+1,maxRob.cbegin()+i-1));//[b,c)
}
int res=*(max_element(maxRob.cbegin()+1,maxRob.cend()));
maxRob[0]=nums[0];
maxRob[1]=nums[1];
for(int i=2;i<sz-1;++i)//get without nums[sz-1]
{
maxRob[i]=nums[i]+*(max_element(maxRob.cbegin(),maxRob.cbegin()+i-1));//[b,c)
}
return max(res,*(max_element(maxRob.cbegin(),maxRob.cend()-1)));
}
};
337. House Robber III
The thief has found himself a new place for his thievery again. There is only one entrance to this area, 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.
Example 1:
3
/ \
2 3
\ \
3 1
Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
Example 2:
3
/ \
4 5
/ \ \
1 3 1
Maximum amount of money the thief can rob = 4 + 5 = 9.
DFS解决问题,其实思路和上一题很相似,我们可以求出两种情况,第一个是父节点与孩纸节点的子树和求出最大,第二是两个孩子节点和求出最大,两者选出最大就可以了。
这一题我跑偏了,看了别人的写出来的。
第一种(注释掉的):多次进入递归,使用map防止重复遍历。
第二种:两次进入递归,用数组存储每次递归进入返回的值。
Reference:
https://discuss.leetcode.com/topic/39834/step-by-step-tackling-of-the-problem
第二种方式有很多变形,比较简单的如
此题中的来说让人深思,原来二叉树遍历还可以玩出这么多花样。还要努力学习才行。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
// int robMap(TreeNode* root,map<TreeNode*,int> &mti)
// {
// if(!root) return 0;
// //遇到已经遍历过的子树,直接返回
// if(mti.find(root)!=mti.end()) return mti[root];
// int res{0};
// if(root->left)
// {
// res+=(robMap(root->left->left,mti)+robMap(root->left->right,mti));
// }
// if(root->right)
// {
// res+=(robMap(root->right->left,mti)+robMap(root->right->right,mti));
// }
// res=max(res+root->val,robMap(root->left,mti)+robMap(root->right,mti));
// mti[root]=res;
// return res;
// }
array<int,2> robSum(TreeNode *root)
{
array<int,2> res{};
if(!root) return res;
array<int,2> left=robSum(root->left);
array<int,2> right=robSum(root->right);
res[0]=max(left[0],left[1])+max(right[0],right[1]);//此时,res[1]远离父节点
res[1]=root->val+left[0]+right[0];//此时,res[1]与父节点保持联系
return res;
}
int rob(TreeNode* root) {
array<int,2> res=robSum(root);
return max(res[0],res[1]);
// map<TreeNode*,int> mti;
// return robMap(root,mti);
}
};