1.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.
动态规划
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])
dp[i]表示到第i个房子最多抢的钱
class Solution {
public:
int rob(vector<int>& nums) {
int len = nums.size();
if (len == 0){
return 0;
}
if (len == 1){
return nums[0];
}
vector<int> dp(len);
dp[0] = nums[0];
dp[1] = max(nums[0], nums[1]);
for (int i = 2; i < len; i++){
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]);
}
return dp[len - 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 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.
dp[i]表示从叶子节点到第i层可以投的最多值。则dp[i]=max(dp[i-2],i-val,dp[i-1]);
数组rob来存储。rob[1]存储的是从叶子节点到当前节点抢劫的最大值,rob[0]存储的是从叶子节点到当前节点的左右孩子层节点抢劫到的最大值:
/**
* 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) {
vector<int> res = dfs(root);
return res[1];
}
private:
vector<int> dfs(TreeNode* root){
vector<int> rob = { 0, 0 };
if (root){
vector<int> left = dfs(root->left);
vector<int> right = dfs(root->right);
rob[0] = left[1] + right[1];
rob[1] = max(rob[0], left[0] + right[0] + root->val);
}
return rob;
}
};
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.
因为是循环的,所以第0个和第n-1个房子不可以同时偷。
因此这个问题等价于偷0~n-2房子或1~n-1房子的最大值。
class Solution {
public:
int rob(vector<int>& nums) {
int len = nums.size();
if (len == 0) return 0;
if (len == 1) return nums[0];
if (len == 2) return max(nums[0], nums[1]);
return max(helper(nums, 0, len - 2), helper(nums, 1, len - 1));
}
private:
int helper(vector<int>&nums,int l,int r){
int pre = 0, cur = 0;
for (int i = l; i <= r; i++){
int tmp = max(pre + nums[i], cur);
pre = cur;
cur = tmp;
}
return cur;
}
};