Leetcode: 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://discuss.leetcode.com/topic/39834/step-by-step-tackling-of-the-problem

rob(root) which will return the maximum amount of money that we can rob for the binary tree rooted at "root"

rob(root) = max(root.val+rob(gradchildren), rob(left)+rot(right)),

 

 think  about the possibilities of overlapping of the subproblems. For example, to obtain rob(root), we need rob(root.left), rob(root.right), rob(root.left.left), rob(root.left.right), rob(root.right.left), rob(root.right.right); but to get rob(root.left), we also needrob(root.left.left), rob(root.left.right), similarly for rob(root.right). The naive solution above computed these subproblems repeatedly, which resulted in bad time performance. Now if you recall the two conditions for dynamic programming: "optimal substructure" + "overlapping of subproblems", we actually have a DP problem. A naive way to implement DP here is to use a hash map to record the results for visited subtrees.

 

8ms DP solution:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     Map<TreeNode, Integer> map;
12     public int rob(TreeNode root) {
13         map = new HashMap<TreeNode, Integer>();
14         return helper(root);
15     }
16     
17     public int helper(TreeNode cur) {
18         if (cur == null) return 0;
19         if (map.containsKey(cur)) return map.get(cur);
20         
21         int val = 0; //initialize
22         if (cur.left != null) {
23             val += helper(cur.left.left) + helper(cur.left.right);
24         }
25         if (cur.right != null) {
26             val += helper(cur.right.left) + helper(cur.right.right);
27         }
28         int curOpt = Math.max(val+cur.val, helper(cur.left)+helper(cur.right));
29         map.put(cur, curOpt);
30         return curOpt;
31     }
32 }

 

 

 for each tree root, there are two scenarios: it is robbed or is not. rob(root) does not distinguish between these two cases, so "information is lost as the recursion goes deeper and deeper", which resulted in repeated subproblems.

 

Redefine rob(root)as a new function which will return an array of two elements, the first element of which denotes the maximum amount of money that can be robbed if "root" is not robbed, while the second element signifies the maximum amount of money robbed if root is robbed.

 

2ms DP solution:

 1 public int rob(TreeNode root) {
 2     int[] res = robSub(root);
 3     return Math.max(res[0], res[1]);
 4 }
 5 
 6 private int[] robSub(TreeNode root) {
 7     if (root == null) {
 8         return new int[2];
 9     }
10     
11     int[] left = robSub(root.left);
12     int[] right = robSub(root.right);
13     
14     int[] res = new int[2];
15     res[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
16     res[1] = root.val + left[0] + right[0];
17     
18     return res;
19 }

 

乐播投屏是一款简单好用、功能强大的专业投屏软件,支持手机投屏电视、手机投电脑、电脑投电视等多种投屏方式。 多端兼容与跨网投屏:支持手机、平板、电脑等多种设备之间的自由组合投屏,且无需连接 WiFi,通过跨屏技术打破网络限制,扫一扫即可投屏。 广泛的应用支持:支持 10000+APP 投屏,包括综合视频、网盘与浏览器、美韩剧、斗鱼、虎牙等直播平台,还能将央视、湖南卫视等各大卫视的直播内容一键投屏。 高清流畅投屏体验:腾讯独家智能音画调校技术,支持 4K 高清画质、240Hz 超高帧率,低延迟不卡顿,能为用户提供更高清、流畅的视觉享受。 会议办公功能强大:拥有全球唯一的 “超级投屏空间”,扫码即投,无需安装。支持多人共享投屏、远程协作批注,PPT、Excel、视频等文件都能流畅展示,还具备企业级安全加密,保障会议资料不泄露。 多人互动功能:支持多人投屏,邀请好友加入投屏互动,远程也可加入。同时具备一屏多显、语音互动功能,支持多人连麦,实时语音交流。 文件支持全面:支持 PPT、PDF、Word、Excel 等办公文件,以及视频、图片等多种类型文件的投屏,还支持网盘直投,无需下载和转格式。 特色功能丰富:投屏时可同步录制投屏画面,部分版本还支持通过触控屏或电视端外接鼠标反控电脑,以及在投屏过程中用画笔实时标注等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值