zoukankan      html  css  js  c++  java
  • Leetcode 337打家劫舍 III

    题目定义:

    在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可行窃的地区。这个地区只有一个入口,我们称之为“根”。
     除了“根”之外,每栋房子有且只有一个“父“房子与之相连。一番侦察之后,聪明的小偷意识到“这个地方的所有房屋的排列类似于一棵二叉树”。 
    如果两个直接相连的房子在同一天晚上被打劫,房屋将自动报警。
    
    计算在不触动警报的情况下,小偷一晚能够盗取的最高金额。
    
    示例 1:
    输入: [3,2,3,null,3,null,1]
    
         3
        / 
       2   3
            
         3   1
    
    输出: 7 
    解释: 小偷一晚能够盗取的最高金额 = 3 + 3 + 1 = 7.
        
    示例 2:
    输入: [3,4,5,1,3,null,1]
    
         3
        / 
       4   5
      /     
     1   3   1
    
    输出: 9
    解释: 小偷一晚能够盗取的最高金额 = 4 + 5 = 9.
    
    

    题目解析:

    使用爷爷、两个孩子、4 个孙子来说明问题
    首先来定义这个问题的状态
    爷爷节点获取到最大的偷取的钱数呢

    首先要明确相邻的节点不能偷,也就是爷爷选择偷,儿子就不能偷了,但是孙子可以偷
    二叉树只有左右两个孩子,一个爷爷最多 2 个儿子,4 个孙子
    根据以上条件,我们可以得出单个节点的钱该怎么算
    4 个孙子偷的钱 + 爷爷的钱 VS 两个儿子偷的钱 哪个组合钱多,就当做当前节点能偷的最大钱数。这就是动态规划里面的最优子结构

    方式一(动态规划初步):

    class Solution {
        public int rob(TreeNode root) {
            if(root == null)
                return 0;
            int money = root.val;
            if(root.left != null)
                money +=(rob(root.left.left) + rob(root.left.right));
            if(root.right != null)
                money +=(rob(root.right.left) + rob(root.right.right));
            return Math.max(money,rob(root.left) + rob(root.right));
        }
    }
    

    方式二(记忆化动态规划):

    class Solution {
        public int rob(TreeNode root) {
            HashMap<TreeNode,Integer> memo = new HashMap<>();
            return robInternal(root,memo);
        }
        private int robInternal(TreeNode root,HashMap<TreeNode,Integer> memo){
            if(root == null)
                return 0;
            if(memo.containsKey(root))
                return memo.get(root);
            int money = root.val;
            if(root.left != null){
                money += (robInternal(root.left.left,memo)+robInternal(root.left.right,memo));
            }
            if(root.right != null){
                money += (robInternal(root.right.left,memo) + robInternal(root.right.right,memo));
            }
            int result = Math.max(money,robInternal(root.left,memo)+ robInternal(root.right,memo));
            memo.put(root,result);
            return result;
        }
    }
    

    方式三():

    /*
    思路改进:
    我们换一种办法来定义此问题
    
    每个节点可选择偷或者不偷两种状态,根据题目意思,相连节点不能一起偷
    
    当前节点选择偷时,那么两个孩子节点就不能选择偷了
    当前节点选择不偷时,两个孩子节点只需要拿最多的钱出来
    就行(两个孩子节点偷不偷没关系)
    我们使用一个大小为 2 的数组来表示 
    int[] res = new int[2] 0 代表不偷,1 代表偷
    任何一个节点能偷到的最大钱的状态可以定义为
    当前节点选择不偷:
    	当前节点能偷到的最大钱数 = 左孩子能偷到的钱 + 右孩子能偷到的钱
    当前节点选择偷:
    	当前节点能偷到的最大钱数 = 	左孩子选择自己不偷时能得到的钱 +
        						右孩子选择不偷时能得到的钱 + 
        						当前节点的钱数
    
    */
    public int rob(TreeNode root) {
        int[] result = robInternal(root);
        return Math.max(result[0], result[1]);
    }
    
    public int[] robInternal(TreeNode root) {
        if (root == null) return new int[2];
        int[] result = new int[2];
    
        int[] left = robInternal(root.left);
        int[] right = robInternal(root.right);
    
        result[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
        result[1] = left[0] + right[0] + root.val;
    
        return result;
    }
    

    参考:

    https://leetcode-cn.com/problems/house-robber-iii/solution/san-chong-fang-fa-jie-jue-shu-xing-dong-tai-gui-hu/


  • 相关阅读:
    spring @Transactional 事务注解
    vue 父子组件的方法调用
    spring boot使用TestRestTemplate集成测试 RESTful 接口
    JS实现网站内容的禁止复制和粘贴、另存为
    vue把localhost改成ip地址无法访问—解决方法
    spring mvc spring boot 允许跨域请求 配置类
    JIRA安装过程中链接mysql的问题!
    vue开发中v-for在Eslint的规则检查下出现:Elements in iteration expect to have 'v-bind:key' directives
    Linux进程启动/指令执行方式研究
    反弹Shell原理及检测技术研究
  • 原文地址:https://www.cnblogs.com/CodingXu-jie/p/14433710.html
Copyright © 2011-2022 走看看