码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode: House Robber III

时间:2016-11-23 12:41:31      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:math   children   span   parent   ace   ted   nts   case   forms   

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 }

 

Leetcode: House Robber III

标签:math   children   span   parent   ace   ted   nts   case   forms   

原文地址:http://www.cnblogs.com/EdwardLiu/p/6092941.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!