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.
思想:nums[i]表示偷取i时,能获得的最大收益,那么必然是先偷[0,1,....i-2]中的一个,再偷num[i]
premax表示[0,1,....,i-2]中的能偷取的最大利益。res 表示能获得的最大收益。
举例:[1,8,2,1,100]
[1,8,3,9,108]
class Solution {
public:
int rob(vector<int>& nums) {
if(nums.size()==0) return 0;
else if(nums.size()==1) return nums[0];
else if(nums.size()==2) return max(nums[0],nums[1]);
int premax=nums[0];
int res=max(nums[0],nums[1]);
for (int i = 2; i < nums.size(); ++i)
{
if(nums[i-2]>premax) premax=nums[i-2];
nums[i]+=premax;
res=max(res,nums[i]);
}
return res;
}
};
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.
思想:有环。即:
抢了第一家就不能抢最后一家,[1,2,...,n-1]
或者抢了最后一家就不能抢第一家,[0,1,2....,n-2]
或者两者都不抢,[1,2,....,n-2],这种情况明显是属于上两种的,可以不用多余考虑。
class Solution {
public:
int rob(vector<int>& nums) {
if(nums.size()==0) return 0;
else if(nums.size()==1) return nums[0];
vector<int> a(nums.begin()+1,nums.end());
vector<int> b(nums.begin(),nums.end()-1);
return max(robOrigin(a),robOrigin(b));
}
int robOrigin(vector<int>& nums) {
if(nums.size()==0) return 0;
else if(nums.size()==1) return nums[0];
else if(nums.size()==2) return max(nums[0],nums[1]);
int premax=nums[0];
int res=INT_MIN;
for (int i = 2; i < nums.size(); ++i)
{
if(nums[i-2]>premax) premax=nums[i-2];
nums[i]+=premax;
res=max(res,nums[i]);
}
return res;
}
};
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 1Maximum 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.
思路:两个变量ret[0]与ret[1],
ret[0]表示偷取当前节点所获得的最大收益,那么肯定不能偷取两个孩子节点,所以有ret[0]=leftret[1]+rightret[1]+root->val;
ret[1]表示不偷取当前节点所获得的最大收益,那么,孩子节点可偷取或者可不偷取,这时取最大值即可。
/**
* 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 rob(TreeNode* root) {
return max(getmaxrob(root)[0],getmaxrob(root)[1]);
}
vector<int> getmaxrob(TreeNode*root){
vector<int> ret(2,0);
if(!root) return ret;
vector<int> leftret=getmaxrob(root->left);
vector<int> rightret=getmaxrob(root->right);
ret[0]=leftret[1]+rightret[1]+root->val;
ret[1]=max(leftret[0],leftret[1])+max(rightret[0],rightret[1]);
return ret;
}
};