zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    转:无线AP模式之无线AP Client客户端模式的应用体验(一)
    Bridge mode
    无线组网(六)——11n无线路由器WDS功能应用举例
    NETGEAR无线路由器WDS功能介绍
    Memory Access vs CPU Speed_你真的了解CPU和内存吗?
    Readyboost功能
    分析:新技术解决服务器内三大I/O瓶颈
    TPLINK Mini系列无线路由器设置指南(三)——Repeater模式
    smartbit网络性能测试介绍
    低端路由器和高端路由的区别
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/6092941.html
Copyright © 2011-2022 走看看