LeetCode House Robber III
原题链接在这里:https://leetcode.com/problems/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.
题解:
List some examples and find out this has to be done with DFS.
One or null node is easy to think, thus use DFS bottom-up, devide and conquer.
Then it must return value on dfs. Each dfs needs current node and return [robRoot, notRobRoot], which denotes rob or skip current node.
robRoot = notRobLeft + notRobRight + root.val
notRobRoot = Math.max(robLeft, notRobLeft) + Math.max(robRight, notRobRight).
Time Complexity: O(n).
Space: O(logn). 用了logn层stack.
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int rob(TreeNode root) {
int [] res = dfs(root);
return Math.max(res[0], res[1]);
}
private int [] dfs(TreeNode root){
int [] dp = new int[2];
if(root == null){
return dp;
}
int [] left = dfs(root.left);
int [] right = dfs(root.right);
//dp[0]表示偷root的, 那么左右都不能偷, 所以用left[1], right[1].
dp[0] = left[1] + right[1] + root.val;
//dp[1]表示不偷root的, 那么左右偷不偷都可以, 取最大值
dp[1] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
return dp;
}
}
类似House Robber, House Robber II.
LeetCode 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 337. House Robber III
337. House Robber III Total Accepted: 18475 Total Submissions: 47725 Difficulty: Medium The thief 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 II 打家劫舍之二
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- [LeetCode] House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- [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 House Robber
原题链接在这里:https://leetcode.com/problems/house-robber/ 题目: You are a professional robber planning to ro ...
- 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] 337. House Robber III 打家劫舍之三
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
随机推荐
- 简单Java算法程序实现!斐波那契数列函数~
java编程基础--斐波那契数列 问题描述:一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 思路:可能出现的情况:(1) n=1 ,一种方法 ;(2)n=2 ...
- [Unity3D]Unity+Android交互教程——让手机"动"起来
想要用Unity实现一个二维码扫描的功能,然后网上找插件,找到一个貌似叫EasyCodeScanner,但下载下来用用,真不好使,一导入运行就报错,调好错了再运行发现点按钮没反应,反复试了几遍发现还是 ...
- 根据google地图抓去全国信息- 抓去全国小区以及新建楼盘信息
本案例由于google每天每个账户能post20000次所以我们需要相对较长的时间来抓去google的数据信息. 主要思路:通过一定的zoom一个相对较大的zoom.我们尽可能的搜索我们的所有数据. ...
- windows phone 摄像头得到图片是旋转90°
我上个随笔讲到,windows phone 拍出来的photo如果直接使用是反转了90°的. 研究了很久..终于发现问题.其实..这是使用习惯问题... CameraCaptureUI 得到的phot ...
- wordpress电子商务插件和主题的使用方法
前提步骤:卸载wordpress干净,需要把相应的数据库删除:drop databade **: (1)先改wordpress中重要文件的权限:777 (2)用usradd -d www /html命 ...
- requirejs的用法(二)
这个系列的第一部分和第二部分,介绍了Javascript模块原型和理论概念,今天介绍如何将它们用于实战. 我采用的是一个非常流行的库require.js. 一.为什么要用require.js? 最早的 ...
- bzoj1468 Tree
最经典的点分治题目,在递归子树的时候减去在算父亲时的不合法方案. #include<iostream> #include<cstdio> #include<cstring ...
- .NET 二维码生成(ThoughtWorks.QRCode)
引用ThoughtWorks.QRCode.dll (源代码里有) 1.简单二维码生成及解码代码: //生成二维码方法一 private void CreateCode_Simple(string n ...
- Android SDK下载和更新失败的解决方法
解决国内访问Google服务器的困难启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『Android SDK Manager - Se ...
- Android服务(Service)研究
Service是android四大组件之一,没有用户界面,一直在后台运行. 为什么使用Service启动新线程执行耗时任务,而不直接在Activity中启动一个子线程处理? 1.Activity会被用 ...