zoukankan      html  css  js  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 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

      3
     / 
    2   3
         
      3   1
    

    Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.

        3
       / 
      4   5
     /     
    1   3   1
    

    Maximum amount of money the thief can rob = 4 + 5 = 9.

     

    Unlike House Robber I && II,  this problem does not have a linear structure. Since it has a binary tree 

    in this problem, we should naturally think about solving the subproblems recursively before solving the

    original problem. 

     

    Optimal substructure:

    f(node) = Math.max(node.val + f(node.left.left) + f(node.left.right) + f(node.right.left) + f(node.right.right),  //choose node

              f(node.left) + f(node.right));  //not choose node

    With this formula, we have the following TLE solution.

    Why TLE? Think about BUD(bottlenecks, unnecessary work, duplicated work)

    To calculate f(node), we need to calculate f(node.left), f(node.right), f(node.left.left), f(node.left.right), f(node.right.left), f(node.right.right);

    To calculate f(node.left), we'll calculate f(node.left.left) and f(node.left.right) again; This duplication pattern applies to each node.

    So we have the overlapping subproblem issue.

     1 public class Solution {
     2     public int houseRobber3(TreeNode root) {
     3         if(root == null){
     4             return 0;
     5         }
     6         int val = 0;
     7         if(root.left != null){
     8             val += houseRobber3(root.left.left) + houseRobber3(root.left.right);
     9         }
    10         if(root.right != null){
    11             val += houseRobber3(root.right.left) + houseRobber3(root.right.right);
    12         }
    13         return Math.max(root.val + val, houseRobber3(root.left) + houseRobber3(root.right));
    14     }
    15 }

     

    To avoid duplicated calculations, we apply the memoization search concept.  For each node, we save the intermediate result

    with choosing the current node and without choosing the current node.

    Essentially this solution applies the dynamic programming principle. We use the top-down memoization search since the it 

    is hard to find a proper init state and recurring bottom up is difficult for binary tree. The natural access fashion for any tree

    structure is top down.

     1 /**
     2  * Definition of TreeNode:
     3  * public class TreeNode {
     4  *     public int val;
     5  *     public TreeNode left, right;
     6  *     public TreeNode(int x) { val = x; }
     7  * }
     8  */
     9 public class Solution {
    10     private class ResultType{
    11         int maxWithCurrNode;
    12         int maxWithoutCurrNode;
    13         ResultType(int v1, int v2){
    14             maxWithCurrNode = v1;
    15             maxWithoutCurrNode = v2;
    16         }
    17     }
    18     public int houseRobber3(TreeNode root) {
    19         ResultType result = dfs(root);
    20         return Math.max(result.maxWithCurrNode, result.maxWithoutCurrNode);
    21     }
    22     private ResultType dfs(TreeNode node){
    23         if(node == null){
    24             return new ResultType(0, 0);
    25         }
    26         ResultType left = dfs(node.left);
    27         ResultType right = dfs(node.right);
    28         ResultType curr = new ResultType(0, 0);
    29         curr.maxWithCurrNode = left.maxWithoutCurrNode + right.maxWithoutCurrNode + node.val;
    30         curr.maxWithoutCurrNode = Math.max(left.maxWithoutCurrNode, left.maxWithCurrNode)
    31                                 + Math.max(right.maxWithoutCurrNode, right.maxWithCurrNode);
    32         return curr;
    33     }
    34 }

     

     

    Related Problems

    Binary Tree Maximum Path Sum

  • 相关阅读:
    eCryptfs文件系统测试
    体验企业级网络管理软件(图片加视频)
    基于Linux2.6内核的加密容器法保护文件方法
    Orion Network Performance Monitor 软件在网络管理中的应用
    两款硬件代理服务器产品之比较
    3Com Network Supervisor与IBM Tivoli NetView两款网管软件操作视频
    成为IBM精英讲师-一分耕耘 一份收获 同时也多了一份责任!
    Hp Open View安装使用视频
    在Fedora 14 alpha 下测试Kvm情况(视频)
    ACM模板——分数类
  • 原文地址:https://www.cnblogs.com/lz87/p/6955071.html
Copyright © 2011-2022 走看看