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]);
}
};

337. House Robber III(包含I和II)的更多相关文章

  1. leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)

    House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...

  2. Leetcode 337. House Robber III

    337. House Robber III Total Accepted: 18475 Total Submissions: 47725 Difficulty: Medium The thief ha ...

  3. [LeetCode] 337. House Robber III 打家劫舍之三

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  4. [LeetCode] 337. House Robber III 打家劫舍 III

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  5. Java [Leetcode 337]House Robber III

    题目描述: The thief has found himself a new place for his thievery again. There is only one entrance to ...

  6. 【leetcode】337. House Robber III

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  7. 337. House Robber III——树的题目几乎都是BFS、DFS,要么递归要么循环

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  8. 337. House Robber III

    二刷吧..不知道为什么house robbery系列我找不到笔记,不过印象中做了好几次了. 不是很难,用的post-order做bottom-up的运算. 对于一个Node来说,有2种情况,一种是选( ...

  9. LeetCode OJ 337. House Robber III

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

随机推荐

  1. Vue中使用computed与watch结合实现数据变化监听

    目的:当数据变化时,为其中重要数据增加边框,实现闪烁以达到提醒目的.数据格式如下,只有在未处理火警/故障时增加闪烁边框.可以使用watch进行深度监听.数据格式已定,也非常明确要监听的数据是有两个.既 ...

  2. Dojo的on函数(以前的dojo.connect)

    ​同jQuery的on函数: require(["esri/map", "dojo/on"], function(Map, on) { // ... on(my ...

  3. db2的定时备份

    定时任务: db2.bat db2cmd -i -w db2_backup.bat exit db2_backup.bat db2 connect to TEST db2 force applicat ...

  4. Bootstrap历练实例:语境色彩的面板

    带语境色彩的面板 使用语境状态类 panel-primary.panel-success.panel-info.panel-warning.panel-danger,来设置带语境色彩的面板,实例如下: ...

  5. 【转】MFC消息映射详解(整理转载)

    消息:主要指由用户操作而向应用程序发出的信息,也包括操作系统内部产生的消息.例如,单击鼠标左按钮,windows将产WM_LBUTTONDOWN消息,而释放鼠标左按钮将产生WM_LBUTTONUP消息 ...

  6. jenkins+maven+svn 自动化部署

    背景: 公司的web平台使用JAVA写的,但是不是用Tomcat部署的,代码内部自带了Web服务器,所以只需要有JAVA环境,将代码打包上传,启动脚本就可以. 项目是根据pom.xml打包成的是.zi ...

  7. word域代码判断奇偶插入分页符

    阿拉伯数字页码判断奇偶插入分页符(PAGE表示当前页码,QUOTE 12表示插入分页符) {IF{=MOD({PAGE},2)} = 1 "{ QUOTE 12}" " ...

  8. 【nginx】nginx.sh nginx 安装脚本

    #! /bin/shcd /usr/local/srcwget http://nginx.org/download/nginx-1.10.1.tar.gzecho 'download success' ...

  9. Python函数的基本定义和调用以及内置函数

    首先我们要了解Python函数的基本定义: 函数是什么? 函数是可以实现一些特定功能的小方法或是小程序.在Python中有很多内建函数,当然随着学习的深入,你也可以学会创建对自己有用的函数.简单的理解 ...

  10. Python语言程序设计之一--for循环中累加变量是否要清零

    最近学到了Pyhton中循环这一章.之前也断断续续学过,但都只是到了函数这一章就停下来了,写过的代码虽然保存了下来,但是当时的思路和总结都没有记录下来,很可惜.这次我开通了博客,就是要把这些珍贵的学习 ...