Problem overview
The method rob receives an int[] named nums, where nums[i] is the money in house i. Two neighboring houses can't both be robbed, and the method returns the largest total you can collect. The constraints are 1 to 100 houses, each holding 0 to 400.
For nums = [2, 7, 9, 3, 1] the answer is 12: robbing houses 0, 2 and 4 gives 2 + 9 + 1.
Approach: skip or rob, house by house
Trying every allowed subset of houses is far too slow, since the number of subsets doubles with every house. Two natural shortcuts are wrong as well. Robbing every other house fails for [2, 1, 1, 2]: both the even and the odd houses total 3, but robbing houses 0 and 3 gives 4. Greedily grabbing the richest house fails too, since it can block two neighbors that are worth more together.
The way out is to decide one house at a time, using only the best totals for the houses before it. For house i there are two choices:
- Skip it. The best total is the best for the houses before it, which we call
curr. - Rob it. Then house
i - 1must stay untouched, so its money adds to the best total from the houses up toi - 2, which we callprev.
The better of the two, Math.max(curr, prev + nums[i]), is the best total up to house i. That is the whole algorithm, and each answer reuses two earlier ones, which makes it dynamic programming. Only two earlier answers are ever needed, so two variables replace a table.
Here is the run for [2, 7, 9, 3, 1], showing the values at the start of each round:
| i | nums[i] | prev | curr | best |
|---|---|---|---|---|
| 0 | 2 | 0 | 0 | 2 |
| 1 | 7 | 0 | 2 | 7 |
| 2 | 9 | 2 | 7 | 11 |
| 3 | 3 | 7 | 11 | 11 |
| 4 | 1 | 11 | 11 | 12 |
At i = 3 skipping wins: robbing the 3 would give 7 + 3 = 10, less than the 11 already available. The method returns 12.
Step by step
- Create
prev= 0. It is the best total for the houses up to two before the current one. Before the first house nothing has been robbed. - Create
curr= 0. It is the best total for the houses up to the one just before the current one. - Loop over every index from 0 while
i < nums.length. Each house is decided exactly once. - Compute
bestas the larger of skipping (curr) and robbing (prev + nums[i]), usingMath.max. - Move both totals one house to the right:
prev = currfirst, thencurr = best. The reverse order would overwrite the valueprevneeds. - Return
curr. After the last house it is the best total for the whole street.
The solution in Java
class Solution {
public int rob(int[] nums) {
int prev = 0;
int curr = 0;
for (int i = 0; i < nums.length; i++) {
int best = Math.max(curr, prev + nums[i]);
prev = curr;
curr = best;
}
return curr;
}
}
Java details worth noticing:
Math.maxonints. It takes two values and returns the larger, with no need for anif.- Arrays know their length.
nums.lengthis a field, with no parentheses, unlikeString.length(). bestis local to the loop body. A new variable is declared every round, which is fine for a primitive.- No overflow worries here. At most 100 houses of at most 400 each add up to 40,000, far below the
intlimit of 2,147,483,647.
Complexity
Time: O(n), where n is the number of houses: one pass, with constant work per house.
Space: O(1): three integers, whatever the size of the input. The input array itself is only read, never changed.
Edge cases and common mistakes
- A single house. For
[5]the loop runs once:best = max(0, 0 + 5) = 5, and the method returns 5. - Two houses. For
[3, 10]the answer is 10, the larger of the two, because they are neighbors. The method gives 10. - All zeros.
[0, 0, 0]returns 0. Zero is a legal amount, and the totals simply stay at 0. - Assuming alternate houses. Skipping two houses in a row can pay off. For
[1, 3, 1, 3, 100]the best total is 103, from houses 1 and 4, while houses 0, 2 and 4 give only 102. - Updating in the wrong order.
curr = best; prev = curr;makes both variables equal, and every later round then addsnums[i]to the wrong total.
Testing
[1, 2, 3, 1]gives4.[2, 7, 9, 3, 1]gives12.[2, 1, 1, 2]gives4.[5]gives5.[3, 10]gives10.[0, 0, 0]gives0.
