《慕课网玩转算法面试》笔记及习题解答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);
}
};
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);
}
};
相关文章推荐
- 《慕课网玩转算法面试》笔记及习题解答1
- 《慕课网玩转算法面试》笔记及习题解答5.4.~5.6
- 《慕课网玩转算法面试》笔记及习题解答6.4.~6.6
- 《慕课网玩转算法面试》笔记及习题解答7.4~7.6
- 《慕课网玩转算法面试》笔记及习题解答3.5~3.6
- 《慕课网玩转算法面试》笔记及习题解答9.3
- 《慕课网玩转算法面试》笔记及习题解答2
- 《慕课网玩转算法面试》笔记及习题解答5.1.~5.3
- 《慕课网玩转算法面试》笔记及习题解答8.4~8.5
- 《慕课网玩转算法面试》笔记及习题解答3.1 ~3.4
- 《慕课网玩转算法面试》笔记及习题解答9.8
- 《慕课网玩转算法面试》笔记及习题解答6.7
- 《慕课网玩转算法面试》笔记及习题解答8.1~8.3
- 算法笔记_127:蓝桥杯2017模拟赛-本科组习题解答(Java)
- 算法笔记_121:蓝桥杯第六届省赛(Java语言C组部分习题)试题解答
- Introduction to Algorithms 算法导论 第1章 基础知识 学习笔记及习题解答
- 算法笔记_123:蓝桥杯第七届省赛(Java语言B组部分习题)试题解答
- 算法笔记_120:蓝桥杯第六届省赛(Java语言B组部分习题)试题解答
- Introduction to Algorithms 算法导论 第4章 递归式 学习笔记及习题解答
- 前端跳槽面试技巧(慕课网)笔记二:算法类