← Whiteboard questions

House Robber

Most cash from a row of houses with no two neighbours robbed

DSA, Medium, 1-D DP

Which structure or algorithm, and what does it cost.

A robber walks along a street of houses. The list houses holds the cash in each house. Every pair of neighbouring houses shares an alarm wire, so robbing two neighbours sets off the alarm. Pick the houses to rob so the alarm stays quiet. Return the most cash the robber can take. An empty street gives 0.

Examples

Input houses = [9, 1, 1, 9]
Output 18
Why rob the first and last houses. They are not neighbours. The two middle houses give only 2.
Input houses = [4, 7, 5, 10, 6]
Output 17
Why rob the houses at positions 1 and 3, counting from 0: 7 + 10. The choice 4 + 5 + 6 gives only 15.
Input houses = []
Output 0
Why edge case: there are no houses, so there is no cash.

Constraints

Your answer must contain

Target Aim for O(n) time and O(1) memory.

Hints

Try the problem first. Open one hint at a time.

Hint 1 a nudge

Look at the last house in the street. The robber either robs it or does not. What does each choice leave for the houses before it?

Hint 2 the approach

Let best for the first k houses be the largest total that uses only those houses. Try to write each value from smaller ones.

Hint 3 the key step

Either skip house k and keep the best for k - 1 houses, or rob it and add its cash to the best for k - 2. Keep two values.

Walkthrough

Full walkthrough brute force to best

Brute force

Try every choice of houses with no two neighbours, add each up and keep the largest. The number of valid choices grows like the Fibonacci numbers: about 4 x 10^12 for 60 houses. It cannot handle 100,000 houses.

Better method

Memoised recursion. Let best(i) be the largest total from house i to the end. It is the larger of best(i + 1) (skip house i) and the cash of house i plus best(i + 2) (rob it). Save each value so every house is solved once. The time is O(n), the memory is O(n), and the recursion goes n levels deep.

Best method

Walk along the street and keep two totals: the best total so far if the latest house is skipped, and the best if it is robbed. For the next house, the new skip total is the larger of the two old totals, because skipping it leaves the earlier choice free. The new rob total is the old skip total plus its cash, because robbing it forces the previous house to be skipped. After the last house, the answer is the larger of the two. This is correct because each house is either skipped or robbed, and the two totals cover both cases exactly.

Hand trace

Run on [9, 1, 1, 9]. Keep the pair (skip, take), starting at (0, 0). House 0, cash 9: skip is max(0, 0) = 0 and take is 0 + 9 = 9. Pair (0, 9). House 1, cash 1: skip is max(0, 9) = 9 and take is 0 + 1 = 1. Pair (9, 1). House 2, cash 1: skip is max(9, 1) = 9 and take is 9 + 1 = 10. Pair (9, 10). House 3, cash 9: skip is max(9, 10) = 10 and take is 9 + 9 = 18. Pair (10, 18). The answer is max(10, 18) = 18.

Code

def rob(houses):
    skip, take = 0, 0               # best total so far if the latest house is skipped, or robbed
    for p in houses:
        skip, take = max(skip, take), skip + p
    return max(skip, take)

Time and memory

O(n) time, because each house is handled once. O(1) memory, because only two totals are kept.

Common mistakes

  • Taking the larger of the two alternate groups, the even places or the odd places. On [9, 1, 1, 9] that gives 10, not 18, because the best choice skips two houses in a row.
  • Robbing the richest house first and removing its neighbours. On [5, 6, 5] that gives 6, but the best total is 10.
  • Adding the cash to the best total that already includes the previous house. That lets two neighbours both be robbed.

Related problems

More 1-D DP problems
House Robber: hints, walkthrough and Python solution