House Robber II
Most cash from a circle of houses with no two neighbours robbed
DSA, Medium, 1-D DP
Which structure or algorithm, and what does it cost.
A robber now works a circular street. The list houses holds the cash in each house, and the last house stands next to the first. Neighbouring houses share an alarm wire, so robbing both of any pair sets it off. That includes the last and first pair. Return the most cash the robber can take. An empty street gives 0.
Examples
Constraints
- 0 <= length of
houses<= 100,000. - Each cash is between 0 and 10,000.
Your answer must contain
- The simple method and why it is too slow
- The better method
- The best method and why it is correct
- Code
- Time and memory cost
- A hand trace of example 1
Target Aim for O(n) time and O(1) memory.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
In a circle the first and last houses touch. Can both be robbed? Think about which of them you may use.
Hint 2 the approach
Suppose the first house is never robbed. What problem is left? And if the last house is never robbed?
Hint 3 the key step
Every valid choice leaves out the first or the last house. How many plain-row answers do you need, and how do you combine them? Check a street with one house.
Walkthrough
Full walkthrough brute force to best
Brute force
Try every choice of houses with no two neighbours around the circle, add each up and keep the largest. The number of valid choices grows like the Lucas numbers: about 3.5 x 10^12 for 60 houses. It cannot handle 100,000 houses.
Better method
Memoised recursion with one extra fact: whether the first house was robbed. The state is the position and that flag, so the last house is blocked when the flag is set. There are about 2n states, so this is O(n) time and O(n) memory, and the recursion goes n levels deep.
Best method
The first and last houses cannot both be robbed, so every valid choice leaves out the first house, or the last house, or both. Solve the plain row problem on the houses without the first, then on the houses without the last, and keep the larger answer. Each row problem uses two running totals: the best if the latest house is skipped, and the best if it is robbed. Together the two rows cover every valid choice, because a choice that uses neither end is inside both. A single house has no neighbour, so it is returned directly.
Hand trace
Run on [9, 2, 3, 9]. Without the first house the row is [2, 3, 9]. The pair (skip, take) goes (0, 2), (2, 3), (3, 11), so its best is 11. Without the last house the row is [9, 2, 3]. The pair goes (0, 9), (9, 2), (9, 12), so its best is 12. The answer is max(11, 12) = 12.
Code
def rob_circle(houses):
n = len(houses)
if n == 1:
return houses[0]
def line(lo, hi): # best total using houses lo .. hi - 1 as a plain row
skip, take = 0, 0
for i in range(lo, hi):
skip, take = max(skip, take), skip + houses[i]
return max(skip, take)
return max(line(1, n), line(0, n - 1))Time and memory
O(n) time, because the row method runs twice over the houses. O(1) memory, because the loop uses index ranges and two totals, not copies.
Common mistakes
- Running the row method once on the whole list. On [9, 2, 3, 9] that gives 18, but the first and last houses touch, so the answer is 12.
- Skipping the single-house case. Without it, dropping the first and the last house leaves an empty row and the answer is 0 instead of the cash.
- Dropping both the first and the last house in one run. That misses the choices that use one of the two ends.