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 }

 

转载于:https://www.cnblogs.com/EdwardLiu/p/6092941.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值