House Robber

House Robber I (LC.198)

You plan to rob a neighborhood. The constraint is that you are not allowed to rob adjacent houses. And your objectively is obviously to maximize your earning. Given an array where money[i] = money stored at house_i, return the maximum amount of money you can rob.

Example

Input: [3, 8, 1, 2, 5, 6]
Return: 8 + 2 + 6 = 16

Analysis

Maximization/minimization on an array usually can be solved with DP. So does this one.

Approach

1. define optimal subproblems by prefix
    rob(n) = max amount of money at house n
2. solve by prefix, 2 optionas, rob nth house or skip it
    rob(n) = Math.max(rob(n-1), rob(n-2) + money[n]);
3. topo order
    for i from 2 to n
4. init base cases
    rob(0) = 0
    rob(1) = money[0]
5. answer
    return rob(n)

Code

Notice the recurrence only uses memo[i-1] and memo[i-2]. So we can just use two variables to replace the memo table as long as we update both of them inside the for loop.

Updated Code

Time & Space Complexity

Time - O(n) Space - O(n) (can be reduced to O(1))

House Robber II (LC.213)

Adding an extra constraint: you can only rob either the first house or the last house. The neighborhood is a circle.

Approach

Last updated