Problem overview
The method rob receives a list nums where nums[i] is the money in house i. Robbing two neighbouring houses sets off an alarm, so no two chosen houses may be adjacent. The method returns the largest total you can take. The list has 1 to 100 houses, each holding 0 to 400.
For [2, 7, 9, 3, 1] the answer is 12: houses 0, 2 and 4 give 2 + 9 + 1.
Approach: decide house by house
Choosing every other house isn't always best. In [2, 1, 1, 2] the best total is 4, from the first and last houses, while alternating houses gives only 3. A fixed pattern fails, so the algorithm has to compare options.
Walk through the houses from left to right. At house i there are two choices:
- Skip it. The best total is whatever was best for the houses before it.
- Rob it. Then house
i - 1must stay untouched, so the money adds to the best total for the houses beforei - 1.
The larger of the two is the best total up to house i. Each decision needs only two earlier results, so two running totals are enough.
Step by step
- Create
prev= 0: the best total from the houses up to two houses before the current one. - Create
curr= 0: the best total from the houses up to the one just before the current one. Before the first house nothing has been robbed, so both start at 0. - Loop
ioverrange(len(nums)). Every house is decided once, using only totals for the houses before it. - Compute
bestasmax(curr, prev + nums[i]): skip the house, or rob it. - Move both totals one house to the right:
prevtakescurr, thencurrtakesbest. Reversing the order would overwrite the valueprevneeds. - Return
curr, the best total for the whole street.
The solution in Python
class Solution:
def rob(self, nums: List[int]) -> int:
prev = 0
curr = 0
for i in range(len(nums)):
best = max(curr, prev + nums[i])
prev = curr
curr = best
return curr
Tracing nums = [2, 7, 9, 3, 1] gives these values at the end of each round:
i | nums[i] | prev | curr |
|---|---|---|---|
| 0 | 2 | 0 | 2 |
| 1 | 7 | 2 | 7 |
| 2 | 9 | 7 | 11 |
| 3 | 3 | 11 | 11 |
| 4 | 1 | 11 | 12 |
At i = 3 robbing the house would give 7 + 3 = 10, less than the 11 already available, so the house is skipped and curr stays at 11.
Python details worth knowing: max takes any number of values and returns the largest. range(len(nums)) gives the indices 0 to len(nums) - 1. The type hint List[int] only documents the parameter and doesn't convert anything.
Complexity
Time: O(n) for n houses: one pass with constant work per house. Space: O(1), because only two totals and a few temporaries are kept.
Edge cases and common mistakes
- One house.
[5]returns 5:max(0, 0 + 5). - Two houses.
[3, 1]returns 3, the larger one, because they are neighbours. - Zeros.
[0, 0]returns 0. - Alternating houses.
[2, 1, 1, 2]returns 4, so a rule such as "take the even positions or the odd ones" is wrong. - A big house late in the list.
[1, 3, 1, 3, 100]returns 103: the 100 is added to the best total from before its neighbour. - Adding to
currinstead ofprev.curr + nums[i]would rob two neighbours.
Testing
[1, 2, 3, 1]gives4.[2, 7, 9, 3, 1]gives12.[2, 1, 1, 2]gives4.[5]gives5.[3, 1]gives3.[1, 3, 1, 3, 100]gives103.
