House Robber
House Robber is LeetCode 198 (Medium). Houses stand in a line, and nums[i] is the money in house i. Robbing two houses that are next to each other sets off the alarm, so no two chosen houses may be neighbours. Return the most money you can take.
- There are at most 100 houses, each holding 0 to 400.
- 1 <= nums.length <= 100
- 0 <= nums[i] <= 400
Intuition
Simple picking rules fail here. On [2, 3, 2], grabbing the richest house blocks both 2s, which together are worth 4.
Instead, walk the street once and carry two totals: the best you can have if the current house is robbed, and the best if it is skipped. Those two are all a later house needs: robbing it builds on the skipped total, and skipping it builds on whichever is larger. After the last house, the answer is the larger of the two.
"Choose items from a line, no two neighbours, maximise the sum" is a one-dimensional DP where each step only looks one item back.
Approach
Before reading on, ask what a later house needs to know about the houses before it. Is "was my left neighbour robbed, and what is the best total in each case" enough? Write the two update rules, then run them on [2, 1, 1, 2].
Two ways to carry the best totals
Keep the best total with the current house robbed and with it skipped, and update both per house.
- Constant memory: two numbers.
- No edge guards: one house works as is.
best[i] is the most money from the first i houses: skip house i − 1 or rob it on top of best[i − 2].
- Easy to explain: the classic DP table.
- Keeps history: lets you recover which houses to rob.
Both make the rob-or-skip choice once per house in O(n) time. The steps, code and live demo use the two running totals; the table version follows the demo.
Start before the first house
Set robbed = 0 and skipped = 0: with no houses seen, both totals are zero.
Rob this house on top of a skipped neighbour
The new robbed is skipped + money. Robbing is only allowed when the house before was not robbed, and skipped is the best total for exactly that situation.
Skip this house and keep the better past
The new skipped is max(robbed, skipped). Skipping places no limit on the house before, so keep whichever old total is larger.
Take the larger total at the end
The last house may be robbed or skipped in the best plan, so return max(robbed, skipped).
Why two totals are enough
A future house only cares whether its left neighbour was robbed. Among all plans for the houses so far that end the same way, only the one with the most money can matter later, because any plan that extends a poorer one also extends the richer one.
Code in Python, C++ and Java
Table of best totals
best[i] is the most money from the first i houses, so best[0] = 0 and best[1] = nums[0]. For each later i, either leave house i − 1 alone and keep best[i − 1], or rob it and add its money to best[i − 2].
Common pitfalls
Taking every other house
return max(sum(nums[0::2]), sum(nums[1::2]))
robbed, skipped = skipped + money, max(robbed, skipped)
The best plan can leave a gap of two houses. On [2, 1, 1, 2] both alternating sums are 3, but robbing the two ends gives 4.
Updating the totals one after the other
robbed = skipped + money skipped = max(robbed, skipped)
robbed, skipped = skipped + money, max(robbed, skipped)
The second line then reads the new robbed, which already includes this house, so the next house can be added on top of it. On [1, 2] that returns 3 by robbing both neighbours; the answer is 2. In C++ and Java, save the new robbed total in a temporary first.
Edge cases
[5]After it, robbed is 5 and skipped is 0, so the answer is 5. Versions that fill a table starting at index 1 need a guard here; this one does not.
Complexity
House Robber FAQ
How do I find which houses to rob, not just the total?
Fill the table version, then walk back from i = n. If best[i] == best[i - 1], house i − 1 was not needed, so step to i − 1. Otherwise house i − 1 was robbed, so note it and jump to i − 2. On [2, 7, 9, 3, 1] this picks houses 4, 2 and 0 for 12.
Is this the same as the maximum sum of non-adjacent elements?
Yes. The houses are array items and the alarm rule is "no two neighbours". The same two totals solve both, and so does any variant that only forbids picking items side by side.
What changes if the houses stand in a circle?
Then the first and last houses are neighbours too. That is House Robber II (LeetCode 213): run this line version once without the last house and once without the first, and take the larger result.