Try it first — give it 25 minutes
0/4 revealedStuck? Take one hint at a time. The struggle is where the pattern sticks.
The problem in plain English
Houses stand in a row. nums[i] is the money in house i. You can rob any set of houses, except that you can never rob two neighbours (houses i and i + 1), because the alarms are linked. Return the largest total you can take.
There are 1 to 100 houses, each holding 0 to 400.
Example: nums = [2, 7, 9, 3, 1] → 12. Rob houses 0, 2 and 4: 2 + 9 + 1 = 12.
Start with brute force
Try every set of houses. Throw away any set that contains two neighbours, and keep the best total.
With n houses there are 2ⁿ sets. For n = 100 that's about 10³⁰. Hopeless.
Greedy shortcuts don't work either. "Take every other house" fails on [2, 1, 1, 2]: the even houses give 3 and the odd houses give 3, but robbing the two ends gives 4. "Take the richest house first" fails on [3, 4, 3]: grabbing the 4 blocks both 3s, which together make 6.
Building the intuition
The brute force makes a choice for every house at once. Simplify: make the choice for one house, the last one. Say you're deciding about the last house of the first i.
- Skip it. Then the last house doesn't matter. The best you can do is the best from the first
i − 1houses. - Rob it. You get its money. Its left neighbour is now off-limits, so the rest must come from the first
i − 2houses, and you want the best possible total there too.
Both options ask "what's the best from a shorter street?" That's the same question on a smaller input, so it's DP. Walk through the recipe.
- State.
dp[i]means the most money you can take from the firstihouses (nums[0]tonums[i − 1]). - Recurrence.
dp[i] = max(dp[i − 1], dp[i − 2] + nums[i − 1]). The first term is "skip", the second is "rob". - Base cases.
dp[0] = 0(no houses, no money).dp[1] = nums[0](one house: rob it). - Order.
dp[i]readsdp[i − 1]anddp[i − 2], so fill left to right.
Why "first i houses" instead of "up to house i"? It gives dp[0] a clear meaning (empty street), and the street with one house needs no special case.
The optimal approach
- Make
dpof sizen + 1. Setdp[0] = 0,dp[1] = nums[0]. - For
ifrom 2 ton:skip = dp[i − 1]take = dp[i − 2] + nums[i − 1]dp[i] = max(skip, take)
- Return
dp[n].
The subtle detail: the index shift. dp[i] covers i houses, so the house being decided is nums[i − 1], not nums[i]. Mixing the two is the most common bug here.
Space optimisation. Each cell reads only the two before it, so two variables are enough and space becomes O(1). House Robber II uses exactly that version:
prev2, prev1 = 0, 0 # dp[i-2], dp[i-1]
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1The code
Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.
class Solution:
def rob(self, nums: List[int]) -> int:
n = len(nums)
# dp[i] = the most money you can take from the first i houses (nums[0..i-1])
dp = [0] * (n + 1)
dp[1] = nums[0] # one house: take it
for i in range(2, n + 1):
skip = dp[i - 1] # leave house i-1 alone
take = dp[i - 2] + nums[i - 1] # rob it, so its left neighbour must be skipped
dp[i] = max(skip, take)
return dp[n]class Solution {
public:
int rob(vector<int>& nums) {
int n = nums.size();
// dp[i] = the most money you can take from the first i houses (nums[0..i-1])
vector<int> dp(n + 1, 0);
dp[1] = nums[0]; // one house: take it
for (int i = 2; i <= n; i++) {
int skip = dp[i - 1]; // leave house i-1 alone
int take = dp[i - 2] + nums[i - 1]; // rob it, so its left neighbour must be skipped
dp[i] = max(skip, take);
}
return dp[n];
}
};class Solution {
public int rob(int[] nums) {
int n = nums.length;
// dp[i] = the most money you can take from the first i houses (nums[0..i-1])
int[] dp = new int[n + 1];
dp[1] = nums[0]; // one house: take it
for (int i = 2; i <= n; i++) {
int skip = dp[i - 1]; // leave house i-1 alone
int take = dp[i - 2] + nums[i - 1]; // rob it, so its left neighbour must be skipped
dp[i] = Math.max(skip, take);
}
return dp[n];
}
}/**
* @param {number[]} nums
* @return {number}
*/
var rob = function (nums) {
const n = nums.length;
// dp[i] = the most money you can take from the first i houses (nums[0..i-1])
const dp = new Array(n + 1).fill(0);
dp[1] = nums[0]; // one house: take it
for (let i = 2; i <= n; i++) {
const skip = dp[i - 1]; // leave house i-1 alone
const take = dp[i - 2] + nums[i - 1]; // rob it, so its left neighbour must be skipped
dp[i] = Math.max(skip, take);
}
return dp[n];
};Dry run
nums = [2, 7, 9, 3, 1]
| i | house nums[i − 1] | skip = dp[i − 1] | take = dp[i − 2] + house | dp[i] |
|---|---|---|---|---|
| 0 | — | — | — | 0 (base) |
| 1 | 2 | — | — | 2 (base) |
| 2 | 7 | 2 | 0 + 7 = 7 | 7 |
| 3 | 9 | 7 | 2 + 9 = 11 | 11 |
| 4 | 3 | 11 | 7 + 3 = 10 | 11 |
| 5 | 1 | 11 | 11 + 1 = 12 | 12 |
Final table: dp = [0, 2, 7, 11, 11, 12]. The answer is dp[5] = 12. Trace it back: 12 came from "take" at the last house (the 1) on top of dp[3] = 11, which came from "take" at the 9 on top of dp[1] = 2. So houses 0, 2, 4.
Complexity
- Time: O(n). One pass, constant work per house.
- Space: O(n) for the table, or O(1) with the two-variable version.
Edge cases and traps
- One house.
dp[1] = nums[0]and the loop never runs. Some solutions readnums[1]and crash. - Zeros. Houses can hold 0. Nothing breaks, but greedy reasoning about "always rob" does.
- Thinking
dp[i]means "houseiis robbed". Then the answer isn'tdp[n], it's the max over the last two cells, and the recurrence gets messy. "Best from the firstihouses, robbed or not" is cleaner. - Using
nums[i]instead ofnums[i − 1]. Off by one, and it goes out of bounds on the last step.
Say it in the interview
"Greedy fails here, for example on
[2, 1, 1, 2]. Letdp[i]be the best total from the firstihouses. For the last house I either skip it, givingdp[i − 1], or rob it, giving its money plusdp[i − 2]. I take the max. Base cases aredp[0] = 0anddp[1] = nums[0]. That's O(n) time, and since each step looks two back, I can drop to two variables for O(1) space."
Follow-ups interviewers ask
- "What if the houses are in a circle?" The first and last are now neighbours. Run this twice, once without the first house and once without the last. That's House Robber II.
- "Which houses did you rob?" Keep the full table, then walk back from
dp[n]: ifdp[i] == dp[i − 1]you skipped housei − 1, otherwise you robbed it and jump toi − 2. - "How is this like Maximum Subarray?" Both make a two-way choice per element and carry one running best forward, which is why both shrink to O(1) space.
Practise the same idea
This is Day 80 of a free 90-day plan 🐸
90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.
Get the full roadmap — free