Problem overview
Rob receives an int[] named nums: house i holds nums[i]. You can't rob two neighbouring houses. The method returns the most money you can take.
For {1, 2, 3, 1} the answer is 4: houses 0 and 2 give 1 + 3. For {2, 7, 9, 3, 1} it is 12 (houses 0, 2 and 4: 2 + 9 + 1). For {2, 1, 1, 2} it is 4, from the two ends. Robbing every other house starting at index 1 would give only 3, so you sometimes have to skip two houses in a row.
Approach: rob it or skip it
Go through the houses from left to right and keep the best total so far. At house i there are two choices:
- Skip it. The best total stays what it was after house
i - 1. - Rob it. Then house
i - 1must stay untouched, so you addnums[i]to the best total after housei - 2.
The better of the two is the best total up to house i. Only the last two totals are ever needed, so two variables replace a whole table. It is the same shape as counting stairs, with Math.Max in place of a sum.
Step by step
- Create
prev= 0. It is the best total from the houses up to two before the current one. - Create
curr= 0. It is 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
ifrom 0 whilei < nums.Length. Every house is decided once. - Compute
best = Math.Max(curr, prev + nums[i]). This line is the whole algorithm: skip (curr) or rob (prev + nums[i]). - Move both totals one house to the right:
prev = currfirst, thencurr = best. The reverse order would lose the oldcurr. - Return
curr. After the last house it is the best total for the whole street.
Tracing {2, 7, 9, 3, 1}, best after each house is 2, 7, 11, 11, 12. At house 2, robbing gives 2 + 9 = 11, which beats keeping 7. At house 3, robbing gives 7 + 3 = 10, which loses to 11, so the total stays 11.
The solution in C#
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;
}
}
Details worth knowing:
Length, notCount. An array reports its size with theLengthproperty.Countbelongs toList<T>.Math.Maxtakes two numbers. It lives in theSystemnamespace and returns the larger of its two arguments.- No overflow worry here. With at most 100 houses of at most 400 each, the total is at most 40,000.
Complexity
Time: O(n), one pass over n houses with constant work each.
Space: O(1), as only prev, curr and best are stored.
Edge cases and common mistakes
- One house.
{5}gives 5:Math.Max(0, 0 + 5). - Two houses.
{3, 10}gives 10, the richer one, since you can't take both. - All zeros.
{0, 0}gives 0. - Greedy by parity. Summing the even-indexed houses and the odd-indexed ones and taking the larger fails on
{2, 1, 1, 2}, where the best answer takes indices 0 and 3. - Adding to
curr.curr + nums[i]would rob two neighbours. The robbery must build onprev. - Overwriting in the wrong order. Setting
curr = bestbeforeprev = currmakesprevequal to the new 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}gives0.
