Browse curriculum

House Robber (LeetCode 198)

House Robber (LeetCode 198): maximise the money robbed from a row of houses without robbing two adjacent ones, with a one-dimensional DP.

Problem Understanding

House Robber: houses along a street hold nums[i] money each. Robbing two adjacent houses sets off an alarm. Return the most money you can rob without robbing two neighbours.

Example: [2, 7, 9, 3, 1] returns 12: houses 0, 2 and 4 (2 + 9 + 1).

Attempt 1: Try Every Choice

At each house, recurse twice: once skipping it, once robbing it and jumping two houses ahead. That explores every valid plan (O(2ⁿ)) and solves the same suffix of the street over and over. The repeated subproblems are the sign that dynamic programming applies.

The Intuition: Skip or Rob, One House at a Time

Let dp[i] be the most money from the first i houses. For the last of them, house i - 1, there are only two options:

  1. Skip it: the best is whatever the first i - 1 houses gave: dp[i - 1].
  2. Rob it: its neighbour is off-limits, so add nums[i - 1] to the best of the first i - 2 houses: dp[i - 2] + nums[i - 1].

So dp[i] = max(dp[i - 1], dp[i - 2] + nums[i - 1]), starting from dp[0] = 0 and dp[1] = nums[0]. Fill the table left to right; dp[n] is the answer. Each cell reads only the two before it, which is why two variables are enough in the optimised version.

House Robber Visualization

dp[i] = MOST MONEY FROM THE FIRST i HOUSES

BASE CASES

nums

2

0

7

1

9

2

3

3

1

4

dp

0

0

2

1

·

2

·

3

·

4

·

5

Skip

:

Rob

:

Best

:

 

dp[0] = 0 (no houses). dp[1] = 2: with one house, rob it

Next

House 1 holds 7. Skip it: dp[1] = 2. Rob it: dp[0] + 7 = 7

Free preview: the first 3 of 10 steps

Houses

The houses run along the top and the dp table under them. For each house, the two cells the choice reads: dp[i − 1] to skip and dp[i − 2] to rob: are marked, and the winner fills dp[i]. The last step walks the table backwards to light up the houses the best plan robs.

House Robber Dry Run

Input: [2, 7, 9, 3, 1]

i House (value) Skip dp[i−1] Rob dp[i−2] + value dp[i]
0 - - - 0
1 0 (2) - - 2
2 1 (7) 2 0 + 7 = 7 7
3 2 (9) 7 2 + 9 = 11 11
4 3 (3) 11 7 + 3 = 10 11
5 4 (1) 11 11 + 1 = 12 12

Return dp[5] = 12. Tracing back: dp[5] came from robbing house 4, dp[3] from robbing house 2, dp[1] is house 0: gives the plan 2 + 9 + 1.

House Robber LeetCode Solution

Bottom-up DP over the first i houses.

House Robber Code
1var rob = function(nums) {
2 const n = nums.length;
3 const dp = new Array(n + 1).fill(0);
4 dp[1] = nums[0];
5 for (let i = 2; i <= n; i++) {
6 const skip = dp[i - 1];
7 const take = dp[i - 2] + nums[i - 1];
8 dp[i] = Math.max(skip, take);
9 }
10 return dp[n];
11};

House Robber in Python, Java, C++, TypeScript and C

House Robber in Python
class Solution:
    def rob(self, nums: List[int]) -> int:
        n = len(nums)
        dp = [0] * (n + 1)
        dp[1] = nums[0]
        for i in range(2, n + 1):
            skip = dp[i - 1]
            take = dp[i - 2] + nums[i - 1]
            dp[i] = max(skip, take)
        return dp[n]
House Robber in Java
class Solution {
    public int rob(int[] nums) {
        int n = nums.length;
        int[] dp = new int[n + 1];
        dp[1] = nums[0];
        for (int i = 2; i <= n; i++) {
            int skip = dp[i - 1];
            int take = dp[i - 2] + nums[i - 1];
            dp[i] = Math.max(skip, take);
        }
        return dp[n];
    }
}
House Robber in C++
class Solution {
public:
    int rob(vector<int>& nums) {
        int n = nums.size();
        vector<int> dp(n + 1, 0);
        dp[1] = nums[0];
        for (int i = 2; i <= n; i++) {
            int skip = dp[i - 1];
            int take = dp[i - 2] + nums[i - 1];
            dp[i] = max(skip, take);
        }
        return dp[n];
    }
};
House Robber in TypeScript
function rob(nums: number[]): number {
    const n = nums.length;
    const dp: number[] = new Array(n + 1).fill(0);
    dp[1] = nums[0];
    for (let i = 2; i <= n; i++) {
        const skip = dp[i - 1];
        const take = dp[i - 2] + nums[i - 1];
        dp[i] = Math.max(skip, take);
    }
    return dp[n];
}
House Robber in C
int rob(int* nums, int numsSize) {
    int *dp = calloc(numsSize + 1, sizeof(int));
    dp[1] = nums[0];
    for (int i = 2; i <= numsSize; i++) {
        int skip = dp[i - 1];
        int take = dp[i - 2] + nums[i - 1];
        dp[i] = skip > take ? skip : take;
    }
    int best = dp[numsSize];
    free(dp);
    return best;
}

O(1) Space: Keep Only Two Values

Each dp[i] reads only dp[i − 1] and dp[i − 2], so the table can shrink to two variables:

def rob(nums):
    prev2, prev1 = 0, 0
    for num in nums:
        prev2, prev1 = prev1, max(prev1, prev2 + num)
    return prev1

Same O(n) time, O(1) space. The table version is easier to trace (and to reconstruct which houses were robbed) which is why the walkthrough uses it.

Edge Cases & Common Mistakes

  • One house: dp[1] = nums[0] is the answer; the loop never runs.
  • Two houses: the larger of the two.
  • Robbing every other house: not always optimal, [2, 1, 1, 2] is best as houses 0 and 3.
  • Off-by-one between dp and nums: dp[i] covers houses 0..i-1, so house i - 1 is the one being decided.
  • Houses in a circle: that is House Robber II, the first and last houses become neighbours, so run this twice, once without each end.

Summary

Two choices per house (skip it or rob it and step back two) give dp[i] = max(dp[i - 1], dp[i - 2] + nums[i - 1]). O(n) time, and O(1) space once the table is reduced to two variables.

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Set the base cases

    dp[0] = 0 and dp[1] = nums[0].

  2. Skip or rob

    For each later house, dp[i] = max(dp[i - 1], dp[i - 2] + nums[i - 1]).

  3. Return the last cell

    dp[n] is the most money from all n houses.

Frequently asked questions

What is the recurrence for House Robber?

Let dp[i] be the most money from the first i houses. At house i - 1 you either skip it, keeping dp[i - 1], or rob it and add it to dp[i - 2], the best that ends at least one house earlier. dp[i] = max(dp[i - 1], dp[i - 2] + nums[i - 1]), with dp[0] = 0 and dp[1] = nums[0].


Can House Robber be solved in O(1) space?

Yes. Each dp value only reads the previous two, so two variables are enough: keep prev2 and prev1, and on each house set them to prev1 and max(prev1, prev2 + num). The time stays O(n).


Why not just rob every other house?

Alternating is not always best. In [2, 1, 1, 2], robbing houses 0 and 2 or 1 and 3 gives 3, but robbing houses 0 and 3 gives 4. The DP compares both choices at every house instead of fixing a pattern.

New to the technique behind this problem? Watch it run step by step:

Dynamic Programming Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description