House Robber
228 浏览 | 2020-04-27 | 分类：动态规划,数据结构与算法 | 标签：

## House Robber

### Summary

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 you from robbing each of them is that 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.

### My approuch

``````class Solution:
def rob(self, nums: List[int]) -> int:
if not nums:
return 0
dp0 = 0
dp1 = nums[0]
for d in range(1, len(nums)):
tmp = dp1
dp1 = dp0 + nums[d]
dp0 = max(dp0, tmp)
return max(dp1, dp0)``````

### Other approuch

``````class Solution:
def rob(self, nums: List[int]) -> int:

# if we have no houses to rob, we can't get any money
if not nums:
return 0

# if we only have 1 or 2 houses to rob, we take the max
if len(nums) <= 2:
return max(nums)

# else we create a state array to hold the max amount
# of money that we can accumulat at each house
state = [0 for _ in range(len(nums))]

# we can memoize our first two houses
state[0] = nums[0]
state[1] = nums[1]

# for each house after our first 2, the max amount we can take
# is equal to the max amount found in our state array, plus the
# amount of the current house
for i in range(2, len(nums)):

# we use i-1 because we cannot rob houses adjacent to the
# one we are currently robbing
state[i] = max(state[:i-1]) + nums[i]

return max(state)``````

## House Robber II

### Summary

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.

### My approuch

``````class Solution:
def rob(self, nums: List[int]) -> int:
n = len(nums)
if not nums:
return 0
if n < 4:
return max(nums)
dp0 = [nums[1], nums[0]]
dp1 = [nums[2], nums[0] + nums[2]]
for d in range(3, n):
tmp1 = dp1[0]
tmp2 = dp1[1]
if d < n - 1:
dp1[1] = dp0[1] + nums[d]
else:
dp1[1] = dp0[1]
dp1[0] = dp0[0] + nums[d]
dp0[0] = max(dp0[0], tmp1)
dp0[1] = max(dp0[1], tmp2)
return max(dp1 + dp0)``````

## House Robber III

### Summary

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.

### My approuch

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

class Solution:
def rob(self, root: TreeNode) -> int:
dp = [0, 0]
return max(self.calRob(root, dp))

def calRob(self, root, dp):
if not root:
return (0, 0)
dp[0], dp[1] = max(dp[0], dp[1]), dp[0] + root.val
left = self.calRob(root.left, dp)
right = self.calRob(root.right, dp)

return (max(left) + max(right) ,root.val + left[0] + right[0])``````

email
web

### 全部评论 (共 2 条评论)

Ciara Didomenico
2020-08-13 23:09
Hi,We're wondering if you've considered taking the written content from foreverdana.com and converting it into videos to promote on Youtube? It's another method of generating traffic.There's a 14 day free trial available to you at the following link: https://bit.ly/3gcMViFRegards,
Ciara
Barbara Atyson
2020-08-01 00:14
Hi,We'd like to introduce to you our explainer video service which we feel can benefit your site foreverdana.com.Check out some of our existing videos here: