Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-053

House Robber

Problem

Given the money in each house on a street, return the maximum amount that can be robbed without robbing two adjacent houses.

At each house choose between skipping it, which keeps the previous best, or taking it plus the best total from two houses back. Return 0 for an empty list.

Hint

best(i) = max(best(i-1), best(i-2) + money[i]); only two previous values are needed.

Test cases

case 1
in: [[1,2,3,1]]
expected: 4
case 2
in: [[2,7,9,3,1]]
expected: 12
case 3
in: [[]]
expected: 0
case 4
in: [[5]]
expected: 5
case 5
in: [[2,1,1,2]]
expected: 4
Starter codePython
def rob(nums):
    # Your code here
    pass

5 test cases · run your code against them in the editor