LeetCode #198 Medium

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.
Constraints
  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 400
dynamic-programmingarray
Open on LeetCode ↗
02

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.

How to spot this pattern

"Choose items from a line, no two neighbours, maximise the sum" is a one-dimensional DP where each step only looks one item back.

03

Approach

Try it first

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].

1

Start before the first house

Set robbed = 0 and skipped = 0: with no houses seen, both totals are zero.

2

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.

3

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.

4

Take the larger total at the end

The last house may be robbed or skipped in the best plan, so return max(robbed, skipped).

5

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.

04

Code in Python, C++ and Java

▶1from typing import List
▶2 
▶3 
▶4class Solution:
▶5 def rob(self, nums: List[int]) -> int:
▶6 robbed, skipped = 0, 0
▶7 for money in nums:
▶8 robbed, skipped = skipped + money, max(robbed, skipped)
▶9 return max(robbed, skipped)
houses2071923314robbedskippedbefore0robbed0skippedno house seen: both totals start at 0
robbed0
skipped0
Before the street starts there is no money either way. Each house will get two totals: the best haul if it is robbed and the best if it is skipped.
houses2071923314robbedskipped20rob it0+2=2old skipped + moneyskip itmax00=0better old totalhouse 0 holds 2: rob → 2, skip → 0
money2
robbed2
skipped0
Robbing house 0 is only allowed if house before it was skipped, so it builds on the old skipped total: 0 + 2 = 2. Skipping house 0 leaves the house before it free, so keep the better old total: 0.
houses2071923314robbedskipped2072rob it0+7=7old skipped + moneyskip itmax20=2better old totalhouse 1 holds 7: rob → 7, skip → 2
money7
robbed7
skipped2
Robbing house 1 is only allowed if house 0 was skipped, so it builds on the old skipped total: 0 + 7 = 7. Skipping house 1 leaves the house before it free, so keep the better old total: 2.
houses2071923314robbedskipped2072117rob it2+9=11old skipped + moneyskip itmax72=7better old totalhouse 2 holds 9: rob → 11, skip → 7
money9
robbed11
skipped7
Robbing house 2 is only allowed if house 1 was skipped, so it builds on the old skipped total: 2 + 9 = 11. Skipping house 2 leaves the house before it free, so keep the better old total: 7.
houses2071923314robbedskipped20721171011rob it7+3=10old skipped + moneyskip itmax117=11better old totalhouse 3 holds 3: rob → 10, skip → 11
money3
robbed10
skipped11
Robbing house 3 is only allowed if house 2 was skipped, so it builds on the old skipped total: 7 + 3 = 10. Skipping house 3 leaves the house before it free, so keep the better old total: 11. Here robbing is worse than skipping, but both totals are kept, because a later house may need this one skipped.
houses2071923314robbedskipped207211710111211rob it11+1=12old skipped + moneyskip itmax1011=11better old totalhouse 4 holds 1: rob → 12, skip → 11
money1
robbed12
skipped11
Robbing house 4 is only allowed if house 3 was skipped, so it builds on the old skipped total: 11 + 1 = 12. Skipping house 4 leaves the house before it free, so keep the better old total: 11.
houses2071923314robbedskipped207211710111211answermax1211=12robhouse 0house 2house 4answer: 12 from houses 0, 2, 4
answer12
The last house can end robbed (12) or skipped (11), so the answer is the larger, 12. Tracing the choices back gives houses 0, 2, 4: no two are neighbours, and their money adds up to 12.
05

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].

▶1from typing import List
▶2 
▶3 
▶4class Solution:
▶5 def rob(self, nums: List[int]) -> int:
▶6 n = len(nums)
▶7 best = [0] * (n + 1)
▶8 best[1] = nums[0]
▶9 for i in range(2, n + 1):
▶10 best[i] = max(best[i - 1], best[i - 2] + nums[i - 1])
▶11 return best[n]
06

Common pitfalls

Taking every other house

✗ Wrong
return max(sum(nums[0::2]), sum(nums[1::2]))
✓ Right
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

✗ Wrong
robbed = skipped + money
skipped = max(robbed, skipped)
✓ Right
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.

07

Edge cases

One house, e.g. [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.

08

Complexity

Time
O(n)
Space
O(1)
One pass over the houses with two numbers, whatever the length. The table version below also runs in O(n) but stores n + 1 totals.
09

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.