资讯专栏INFORMATION COLUMN

LeetCode[337] House Robber III

Dr_Noooo / 867人阅读

摘要:复杂度思路对于每一个位置来说,考虑两种情况分别对和再进行计算。用对已经计算过的进行保留,避免重复计算。

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 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.

recursion + Memorization

复杂度
O(N), O(lgN)

思路
对于每一个位置来说,考虑两种情况, Max(child, subchild + root.val).
分别对child和subchild再进行recursion计算。

用map对已经计算过的node进行保留,避免重复计算。

代码

public int rob(TreeNode root) {
    // Base case;
    if(root == null) return 0;
    if(root.left == null && root.right == null) return root.val;
    if(map.containsKey(root)) return map.get(root);
    int child = 0, subchild = 0;
    if(root.left != null) {
        child += rob(root.left);
        subchild += rob(root.left.left) + rob(root.left.right);
    }
    if(root.right != null) {
        child += rob(root.right);
        subchild += rob(root.right.left) + rob(root.right.right);
    }
    int val = Math.max(child, subchild + root.val);
    map.put(root, val);
    return val;
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65225.html

相关文章

  • leetcode337. House Robber III

    摘要:题目要求即如何从树中选择几个节点,在确保这几个节点不直接相连的情况下使其值的和最大。当前节点的情况有两种选中或是没选中,如果选中的话,那么两个直接子节点将不可以被选中,如果没选中,那么两个直接子节点的状态可以是选中或是没选中。 题目要求 The thief has found himself a new place for his thievery again. There is on...

    AlphaWallet 评论0 收藏0
  • LeetCode 337. House Robber III

    摘要:一番侦察之后,聪明的小偷意识到这个地方的所有房屋的排列类似于一棵二叉树。如果两个直接相连的房子在同一天晚上被打劫,房屋将自动报警。计算在不触动警报的情况下,小偷一晚能够盗取的最高金额。 Description The thief has found himself a new place for his thievery again. There is only one entranc...

    ymyang 评论0 收藏0
  • [LintCode/LeetCode] House Robber III

    摘要:解法真的非常巧妙,不过这道题里仍要注意两个细节。中,为时,返回长度为的空数组建立结果数组时,是包括根节点的情况,是不包含根节点的情况。而非按左右子树来进行划分的。 Problem The thief has found himself a new place for his thievery again. There is only one entrance to this area,...

    macg0406 评论0 收藏0
  • leetcode198,213 house robber

    摘要:你不能连着偷两家因为这样会触发警报系统。现在有一个数组存放着每一家中的可偷金额,问可以偷的最大金额为多少这里考验了动态编程的思想。动态编程要求我们将问题一般化,然后再找到初始情况开始这个由一般到特殊的计算过程。 House Robber I You are a professional robber planning to rob houses along a street. Each...

    whidy 评论0 收藏0
  • [LeetCode] House Robber I II

    摘要:注意对边界条件的判断,是否非空,是否长度为 House Robber I Problem You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping y...

    qpal 评论0 收藏0

发表评论

0条评论

Dr_Noooo

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<