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.
动态规划问题。
class Solution {
public:
int rob(vector<int>& nums) {
int n = nums.size();
vector<int> f(n,0);
if(n <=0) return 0;
if(n ==1) return nums[0];
f[0] = nums[0];
f[1] = max(nums[0],nums[1]);
for(int i = 2 ; i< n;i++){
f[i] = max(f[i-1],nums[i]+ f[i-2]);
}
return f[n-1];
}
};
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.
还是个DP问题,因为头和尾相连,只需要在I的基础上分为有头和无头即可。
class Solution {
public:
int robber(vector<int> &nums,int l,int r){
int cur=0,pre=0;
for(int i=l;i<=r;i++){
int temp = max(pre + nums[i],cur);
pre = cur;
cur = temp;
}
return cur;
}
int rob(vector<int>& nums) {
int n = nums.size();
if(n <= 0) return 0;
if(n==1) return nums[0];
if(n==2) return max(nums[0],nums[1]);
return max(robber(nums,0,n-2),robber(nums,1,n-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.
https://leetcode.com/discuss/91899/step-by-step-tackling-of-the-problem
非常好的进阶思路,这里贴出方法二和方法三
方法二:
class Solution {
public:
int robber(TreeNode* root,unordered_map<TreeNode*,int> &maps){
if(!root) return 0;
auto it = maps.find(root);
if(it != maps.end()) return maps[root];
int val = 0;
if(root->left) val += robber(root->left->left,maps) + robber(root->left->right,maps);
if(root->right) val += robber(root->right->left,maps) + robber(root->right->right,maps);
val = max(val+root->val,robber(root->left,maps)+robber(root->right,maps));
maps[root]= val;
return val;
}
int rob(TreeNode* root) {
unordered_map<TreeNode*,int> maps;
return robber(root,maps);
}
};
方法三:
class Solution {
public:
vector<int> robber(TreeNode* root){
if(!root) return vector<int>(2);
vector<int> left = robber(root->left);
vector<int> right = robber(root->right);
std::vector<int> res(2);
res[0] = max(left[0],left[1]) + max(right[0],right[1]);
res[1] = root->val + left[0] + right[0];
return res;
}
int rob(TreeNode* root) {
vector<int> res = robber(root);
return max(res[0],res[1]);
}
};