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:
Input: [3,2,3,null,3,null,1]
3
/ \
2 3
\ \
3 1
Output: 7
Explanation: Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
Example 2:
Input: [3,4,5,1,3,null,1] 3
/ \
4 5
/ \ \
1 3 1 Output: 9
Explanation: Maximum amount of money the thief can rob = 4 + 5 = 9.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
知道是dc,不知道具体怎么写。用dc可以新生成数组,不需要别的参数。因为数组里面的元素只有2个,指定一下就行了。
[英文数据结构或算法,为什么不用别的数据结构或算法]:
数组:因为只有偷与否2种状态,left right res都需要在其中比较,所以开空间为2的数组即可
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
用dc可以新生成数组,不需要别的参数。因为数组里面的元素只有2个,指定一下就行了。
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int rob(TreeNode root) {
//corner case
if (root == null) return 0; //call the robHelper
int[] result = robHelper(root); //compare and return
return Math.max(result[0], result[1]);
} public int[] robHelper(TreeNode root) {
//corner case
if (root == null) return new int[2];
int[] result = new int[2]; //initialization : 2 int[] left and right
int[] left = robHelper(root.left);
int[] right = robHelper(root.right); //define the numbers
//choose root
result[1] = left[0] + root.val + right[0];
//not choose
result[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]); //return
return result;
}
}
337. House Robber III二叉树上的抢劫题的更多相关文章
- 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 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] 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】337. House Robber III 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 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 ...
- 337 House Robber III 打家劫舍 III
小偷又发现一个新的可行窃的地点. 这个地区只有一个入口,称为“根”. 除了根部之外,每栋房子有且只有一个父房子. 一番侦察之后,聪明的小偷意识到“这个地方的所有房屋形成了一棵二叉树”. 如果两个直接相 ...
随机推荐
- php配置php_pdo_mysql模块
网上的都是什么编译安装的,总算找到一个简单的方法 安装好PHP yum install php php-fpm -y 直接安装pdo模块 yum install php-pdo_mysql 在/etc ...
- jQeury 批量删除
/*批量删除*/ function datadel(){ var ids = new Array(); $("input[name='batch']:checked").each( ...
- iOS兼容性(不断完善)
1.iOs不支持iframe,不支持flash,如果移动端要嵌入视频,请用html5 的video标签.
- c#中枚举类型 显示中文
public enum AuditEnum { [Description("未送审")] Holding=0, [Description("审核中")] Aud ...
- redis 4.x 安装哨兵模式 sentinel
1.下载 http://download.redis.io/releases/redis-4.0.11.tar.gz 2.解压 tar zxvf redis-4.0.11.tar.gz 3.安装 cd ...
- Docker系列08:容器监控
1 监控解决方案 cadvisor+influxdb+grafana cAdvisor:Google开源的工具,用于监控Docker主机和容器系统资源,通过图形页面实时显示数据,但不存储:它通过宿主机 ...
- C语言 链表(Dev C++/分文件版)
头文件:quechain.h struct Question { int _id; struct Question* pre; struct Question* next; }; void chain ...
- 消息对话框 MessageBoxButtons
消息对话框MessageBox经常用于向用户显示通知信息.例如,在操作过程中遇到错误或程序异常,经常会使用这种方式给用户以提示,它是特殊类型的对话框. 在C#中,MessageBox消息对话框位于Sy ...
- 44个Java代码性能优化总结
https://blog.csdn.net/xiang__liu/article/details/79321639 ---稍后有时间整理
- Java 学习 注解
转载:https://blog.csdn.net/briblue/article/details/73824058 注解语法 因为平常开发少见,相信有不少的人员会认为注解的地位不高.其实同 class ...