Skip to main content

House Robber

Question

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

Example 1
Input: [2,1,1,2]

Output: 4

Solution

all//House Robber.py


def houseRobber(nums):
if len(nums) == 0: return 0
if len(nums) == 1: return nums[0]
if len(nums) == 2: return max(nums[0], nums[1])

dp = [0] * len(nums)
dp[0] = nums[0]
dp[1] = max(nums[0], nums[1])

for i in range(2, len(nums)):
dp[i] = max(dp[i-1], dp[i-2] + nums[i])
return dp[-1]