Home
DSA Patterns
Dynamic Programming
House Robber
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:
- Skip it: the best is whatever the first
i - 1houses gave:dp[i - 1]. - Rob it: its neighbour is off-limits, so add
nums[i - 1]to the best of the firsti - 2houses: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
0
1
2
3
4
dp
0
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
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
dpandnums:dp[i]covers houses0..i-1, so housei - 1is 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
