Skip to content

House Robber

Choose houses to rob for the most money when you may never rob two neighbors.

Medium1D Dynamic Programming
Solve on LeetCode

The problem

Houses stand in a row and nums[i] is the money in house i. Robbing two houses that are next to each other sets off an alarm. Return the most money you can take without robbing any two neighbors.

Input nums = [6, 1, 2, 7]

Output 13

Rob the first and the last house: 6 + 7. They are not neighbors.

Input nums = [3, 10, 3, 1, 8]

Output 18

Rob houses 1 and 4 for 10 + 8. Taking every other house from the start gives only 3 + 3 + 8 = 14.

How to spot the pattern

  • You pick items from a line, and picking one rules out its neighbors.
  • You want the best total, and each item is a plain take-or-skip choice.

The idea

State: best(i) is the most money you can take from houses 0 to i. At house i you either skip it and keep best(i - 1), or rob it, which rules out house i - 1 and gives best(i - 2) + nums[i]. So best(i) = max(best(i - 1), best(i - 2) + nums[i]), and since each value needs only the two before it, two variables are enough.

Brute force: Try every set of houses that has no two neighbors, which takes O(2^n) time.

Step by step

  1. 1Keep twoBack and oneBack, the best totals up to two houses back and one house back, both starting at 0.
  2. 2For each house, compute here = max(oneBack, twoBack + money): skip it, or rob it on top of the best from two houses back.
  3. 3Shift the pair: twoBack becomes oneBack, and oneBack becomes here.
  4. 4After the last house, return oneBack.

See the pattern move

The 1d dynamic programming pattern on a small example. Step through it, or press play.

Code

class Solution:
    def rob(self, nums: List[int]) -> int:
        # best(i) = most money from houses 0 to i
        # best(i) = max(best(i - 1), best(i - 2) + nums[i])
        two_back, one_back = 0, 0  # best(i - 2) and best(i - 1)
        for money in nums:
            # skip this house, or rob it on top of the best from two houses back
            two_back, one_back = one_back, max(one_back, two_back + money)
        return one_back

Complexity

Time

O(n)

Space

O(1)

One pass over the houses with two running totals.

Watch out for

  • Assuming the answer is all even positions or all odd positions: in [5, 1, 1, 5] the best is 10, which skips two houses in a row.
  • Robbing house i on top of best(i - 1), which may already include house i - 1.

Follow-up questions

What if the houses stand in a circle?

The first and last houses become neighbors. Solve the row once without the first house and once without the last, and take the better. That is House Robber II.

How would you list the houses to rob?

Keep the whole best array, then walk back from the end: if best(i) equals best(i - 1), house i was skipped; otherwise it was robbed and you jump to i - 2.

In Hinglish

Har ghar par do hi option hain: chhod do aur `best(i - 1)` rakho, ya loot lo aur `best(i - 2) + paisa` lo. Jo bada ho, wahi `best(i)`.

Related problems