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.
3
/ \
2 3
\ \
3 1
Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
3
/ \
4 5
/ \ \
1 3 1
Maximum amount of money the thief can rob = 4 + 5 = 9.
这题一开始用递归 结果超时;随机加cache变成DP。。通过了
helper1表示抢root
helper2表示不抢root;此处注意不抢root时 即可以抢root.left和root.right 也可以不抢它们 二取其一 所以注意42-47行
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: The maximum amount of money you can rob tonight
*/
Map<TreeNode, Integer> map1 = new HashMap<TreeNode, Integer>();
Map<TreeNode, Integer> map2 = new HashMap<TreeNode, Integer>();
public int houseRobber3(TreeNode root) {
// write your code here
if(root==null) return 0;
return Math.max(helper1(root), helper2(root));
}
// include root
private int helper1(TreeNode root){
if(map1.containsKey(root)){
return map1.get(root);
}
int sum = root.val;
if(root.left!=null){
sum += helper2(root.left);
}
if(root.right!=null){
sum += helper2(root.right);
}
map1.put(root, sum);
return sum;
}
// not include root
private int helper2(TreeNode root){
if(map2.containsKey(root)){
return map2.get(root);
}
int sum = 0;
if(root.left!=null){
sum += Math.max(helper1(root.left), helper2(root.left));
}
if(root.right!=null){
sum += Math.max(helper1(root.right), helper2(root.right));
}
map2.put(root, sum);
return sum;
}
}
House Robber III的更多相关文章
- [LintCode] House Robber III 打家劫舍之三
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
- Leetcode 337. House Robber III
337. House Robber III Total Accepted: 18475 Total Submissions: 47725 Difficulty: Medium The thief ha ...
- 337. House Robber III(包含I和II)
198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...
- 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:二叉树下的不能相邻,求能 ...
- [LeetCode] House Robber III 打家劫舍之三
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
- LeetCode House Robber III
原题链接在这里:https://leetcode.com/problems/house-robber-iii/ 题目: The thief has found himself a new place ...
- Java [Leetcode 337]House Robber III
题目描述: The thief has found himself a new place for his thievery again. There is only one entrance to ...
- [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 ...
- [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 ...
- 【LeetCode】House Robber III(337)
1. Description The thief has found himself a new place for his thievery again. There is only one ent ...
随机推荐
- nodejs导出excel
//导出Excel var nodeExcel = require("excel-export"); var fs = require("fs"); var c ...
- 如何模拟一个http请求并把response的内容保存下载下来,导出到excel中(结尾福利)
def doExport(self): # 模拟一个http请求 url = u'%s?dumptype=investigation&dumpid=%s&timezone=8' % ( ...
- every day a practice —— morning(6)
"Nearly one in five job ads for China's 2018 national civil service called for 'men only' or 'm ...
- You Don't Know JS: Scope & Closures (附加:Lexical/dynamic作用域)(附加:Lexical-this)
JavaScript只有Lexical Scope 模式 Lexical Scope就是在写代码的时候,定义函数的时候创建的作用域! 而动态作用域是在runtime时,函数被调用的地方的作用域! 实际 ...
- jquery插件中找到好玩插件 http://www.jq22.com/
超实用的angular.js无刷新分页完整案例 http://www.jq22.com/jquery-info14714 js联动选择插件mobileSelect.js http://www.jq22 ...
- 标准化数据-StandardScaler
StandardScaler----计算训练集的平均值和标准差,以便测试数据集使用相同的变换 官方文档: class sklearn.preprocessing.StandardScaler(copy ...
- Gifts by the List CodeForces - 681D (思维)
大意: 给定森林, 要求构造一个表, 满足对于每个$x$, 表中第一次出现的$x$的祖先(包括$x$)是$a_x$. 刚开始还想着直接暴力分块优化一下连边, 最后按拓扑序输出... 实际上可以发现$a ...
- python记录_day17 类与类之间的关系
一.依赖关系 a类的对象是b类方法的参数 这种关系是我用着你,但你不属于我,比如公司和临时工的关系,是很弱的一种关系 class Zhiwu: def __init__(self,name,atk): ...
- Spring Boot项目打包部署到外部Tomcat
1.生成war包 1)修改POM文件,将打包类型改为war:<packaging>war</packaging> <packaging>war</packag ...
- PAT 1050 String Subtraction
1050 String Subtraction (20 分) Given two strings S1 and S2, S=S1−S2 is defined to be t ...