LeetCode 337. House Robber III II

231 篇文章 0 订阅

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:

Input: [3,2,3,null,3,null,1]

     3
    / \
   2   3
    \   \ 
     3   1

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

Example 2:

Input: [3,4,5,1,3,null,1]

     3
    / \
   4   5
  / \   \ 
 1   3   1

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

---------------------------------------------------------------------------------------------------------

这题的例子给的迷惑性挺强,总让人觉得是把不相邻层加在一起就行,结果碰上[4,1,null,2,null,3] WA了,后来考虑了不相邻两层,碰上[2,1,3,null,4]又WA了。然后才反应过来这是一个树形的DP。

但是用DFS+记忆化数组写得时候又写了一个bug,当爷爷节点不存在的时候,儿子节点也不一定存在,codes注释里记录了之前写的问题。

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def dfs(self, root: TreeNode, node_dict) -> int:
        if (root in node_dict):
            return node_dict[root]
        w1,w2 = 0,root.val #w/o root, with root        
        if (root.left):
            l1,l2 = self.dfs(root.left, node_dict)
            w1,w2 = w1+max(l2,l1),w2+l1 #bug: w1,w2 = w1+l2,w2+l1
        if (root.right):
            r1,r2 = self.dfs(root.right, node_dict)
            w1,w2 = w1+max(r2,r1),w2+r1 #bug: w1,w2 = w1+l2,w2+l1
        node_dict[root] = w2,w1
        return w1,w2 #w/o root, with root
    
    def rob(self, root: TreeNode) -> int:
        if (root == None):
            return 0
        node_dict = {}
        v1,v2 = self.dfs(root, node_dict)
        return max(v1,v2)

---------------------------------------------------------------------------------------------------------

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

Example 1:

Input: [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
             because they are adjacent houses.

Example 2:

Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
             Total amount you can rob = 1 + 3 = 4.

---------------------------------------------------------------------------------------------

II相对简单一些,但是反应有些慢。这种题一定先把DP明确了,对于这种有环的情况,时刻铭记状态的问题。有个DP转移后,再想怎么优化到一维。

class Solution:
    def rob(self, nums: List[int]) -> int:
        l = len(nums)
        if (l <= 0):
            return 0
        pre1,cur1,pre2,cur2,res = 0,nums[0],0,0,nums[0]
        #cur2 without head
        
        for i in range(1,l-1):
            tmp1,tmp2 = cur1,cur2
            cur1,cur2 = max(pre1+nums[i],cur1),max(pre2+nums[i],cur2)
            res = max(cur1,cur2,res)
            pre1,pre2 = tmp1,tmp2
        return max(res, nums[l-1]+pre2)
            

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值