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

```
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)
```

```
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)
```

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

```
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)
```

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.

```
# 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])
```

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

Ciara

https://www.youtube.com/watch?v=zvGF7uRfH04

https://www.youtube.com/watch?v=cZPsp217Iik

https://www.youtube.com/watch?v=JHfnqS2zpU8All of our videos are in a similar animated format as the above examples and we have voice over artists with US/UK/Australian accents.They can show a solution to a problem or simply promote one of your products or services. They are concise, can be uploaded to video such as Youtube, and can be embedded into your website or featured on landing pages.Our prices are as follows depending on video length:

0-1 minutes = $169

1-2 minutes = $279

2-3 minutes = $389

3-4 minutes = $489*All prices above are in USD and include a custom video, full script and a voice-over.If this is something you would like to discuss further, don't hesitate to get in touch.

If you are not interested, simply delete this message and we won't contact you again.Kind Regards,

Barbara