Start learning free

LESSON 240 OF 314 · DYNAMIC PROGRAMMING

House Robber in C#: Dynamic Programming with Two Totals

Practice O(n) Read the full explanation

You get an array nums, the money in each house along a street. House i holds nums[i].

Return the most money you can rob.

Houses next to each other share an alarm. So you can never rob two neighbors.

Example 1:

Input: nums = [3, 10, 3, 1, 2]
Output: 12
Explanation: The best pick is house 1 (10) and house 4 (2).
That gives 10 + 2 = 12.
Robbing houses 0, 2 and 4 gives only 3 + 3 + 2 = 8.

Example 2:

Input: nums = [6, 1, 2, 7]
Output: 13
Explanation: Rob house 0 (6) and house 3 (7).
That gives 6 + 7 = 13.
Houses 1 and 2 are both skipped, and that is allowed.

Example 3:

Input: nums = [9]
Output: 9
Explanation: With one house, rob it.

Constraints:

  • 1 to 100 houses.
  • Each house holds from 0 to 400.

STEP 1 OF 6
    Ctrl + Enter to check

    C# · DYNAMIC PROGRAMMING

    House Robber in C#, explained

    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 - 1 must stay untouched, so you add nums[i] to the best total after house i - 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

    1. Create prev = 0. It is the best total from the houses up to two before the current one.
    2. 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.
    3. Loop i from 0 while i < nums.Length. Every house is decided once.
    4. Compute best = Math.Max(curr, prev + nums[i]). This line is the whole algorithm: skip (curr) or rob (prev + nums[i]).
    5. Move both totals one house to the right: prev = curr first, then curr = best. The reverse order would lose the old curr.
    6. 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, not Count. An array reports its size with the Length property. Count belongs to List<T>.
    • Math.Max takes two numbers. It lives in the System namespace 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 on prev.
    • Overwriting in the wrong order. Setting curr = best before prev = curr makes prev equal to the new total.

    Testing

    • {1, 2, 3, 1} gives 4.
    • {2, 7, 9, 3, 1} gives 12.
    • {2, 1, 1, 2} gives 4.
    • {5} gives 5.
    • {3, 10} gives 10.
    • {0, 0} gives 0.
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free