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 ...
随机推荐
- css笔记1: html页面的CSS、DIV命名规则
原地址:http://www.cnblogs.com/rising-fay/archive/2013/02/25/2932592.html CSS命名规则 头:header 内容:content/co ...
- android 第三方登录---新浪微博
1.AndroidManiFest.xml设置,这里我只是简单的用授权,获取基本信息,所以只用了这一个 <!--微博--> <!-- 必须注册在微博授权,分享微博时候用到 --> ...
- Android Telephony —— 手机信号实时变化源码分析过程记录
源码版本:4.4 跳过InCallActivity等UI实现.先看service以及底层. 1, 在frameworks/opt下面会发现如下文件列表: ./telephony/src/java/co ...
- Java小知识--length,length(),size()方法详细介绍
Java中length,length(),size()区别 length属性:用于获取数组长度. eg: int ar[] = new int{1,2,3} /** * 数组用length属性取得长度 ...
- 在Windows10 64位 Anaconda4 Python3.5下安装XGBoost
系统环境: Windows10 64bit Anaconda4 Python3.5.1 软件安装: Git for Windows MINGW 在安装的时候要改一个选择(Architecture选择x ...
- [译]:Orchard入门——给网站添加新博客
原文链接:Adding a Blog to Your Site 文章内容基于Orchard 1.8版本 Orchard提供一个博客引擎--这让添加一个新博客到你网站变得非常容易. 本文将介绍怎样添加一 ...
- WEB容器启动——web.xml加载详解
最近在看spring的源码,关于web.xml文件在容器(Tomcat.JBOSS等)启动时加载顺序问题很混乱,通过搜集资料,得出以下的结论: 1.加载顺序与它们在 web.xml 文件中的先后顺序无 ...
- PHP导出数据到Excel
<?php date_default_timezone_set('PRC'); $filename="info.xls";//先定义一个excel文件 header(&quo ...
- Leetcode Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- Leetcode Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...