House Robber II
House Robber II is LeetCode 213 (Medium). The houses stand in a circle, so house 0 and the last house are next to each other. Robbing any two neighbouring houses sets off the alarm. Given the money in each house, nums, return the most you can take without triggering it.
numscan hold a single house, which you may rob.
- 1 <= nums.length <= 100
- 0 <= nums[i] <= 1000
Intuition
The circle adds exactly one new conflict: the first and last houses cannot both be robbed. So every allowed plan leaves out at least one of the two ends.
That splits the plans into two groups. Plans that leave out the last house live on the straight street nums[0 .. n − 2]. Plans that leave out the first house live on nums[1 .. n − 1]. Neither street wraps around, so the usual no-two-neighbours scan solves each one in a single pass, and the answer is the larger of the two results.
A circular version of a line problem, where only the two ends interact: fix what happens at one end, which turns the circle back into a line, and solve the line once per case.
Approach
Before reading on, take [2, 3, 2] and list every allowed plan. Which end does each plan leave out? Then say which straight street each plan belongs to.
Return the only house when there is one
If nums has one house, return nums[0]. The split into two streets below needs at least two houses.
Scan one straight street by its index range
rob_line(lo, hi) walks houses lo to hi with two numbers: best, the most money from the houses scanned so far, and before, the same value one house earlier. Passing index bounds avoids copying the list.
Pass the house or rob it
For house i, the new best is max(best, before + nums[i]). Passing keeps best. Robbing adds the money to before, which never includes house i − 1, so no two neighbours are taken.
Run the scan twice and keep the larger
Call rob_line(0, n - 2) for the street without the last house and rob_line(1, n - 1) for the street without the first house, and return the bigger result.
Why the two streets cover every plan
An allowed plan never holds both ends. If it leaves out the last house, it is a plan on the first street; if not, it leaves out the first house and is a plan on the second. And neither street contains both ends, so no plan counted there breaks the circle rule.
Code in Python, C++ and Java
before (0), the best total from houses at least two back, so it never touches house -1. That gives 2. Passing keeps 0. Robbing wins, so best becomes 2. The old best moves into before for the next house.before (0), the best total from houses at least two back, so it never touches house 0. That gives 3. Passing keeps 2. Robbing wins, so best becomes 3. The old best moves into before for the next house.before (0), the best total from houses at least two back, so it never touches house 0. That gives 3. Passing keeps 0. Robbing wins, so best becomes 3. The old best moves into before for the next house.before (0), the best total from houses at least two back, so it never touches house 1. That gives 2. Passing keeps 3. Passing is at least as good, so best stays 3. The old best moves into before for the next house.before (0), the best total from houses at least two back, so it never touches house -1. That gives 1. Passing keeps 0. Robbing wins, so best becomes 1. The old best moves into before for the next house.before (0), the best total from houses at least two back, so it never touches house 0. That gives 2. Passing keeps 1. Robbing wins, so best becomes 2. The old best moves into before for the next house.before (1), the best total from houses at least two back, so it never touches house 1. That gives 4. Passing keeps 2. Robbing wins, so best becomes 4. The old best moves into before for the next house.before (0), the best total from houses at least two back, so it never touches house 0. That gives 2. Passing keeps 0. Robbing wins, so best becomes 2. The old best moves into before for the next house.before (0), the best total from houses at least two back, so it never touches house 1. That gives 3. Passing keeps 2. Robbing wins, so best becomes 3. The old best moves into before for the next house.before (2), the best total from houses at least two back, so it never touches house 2. That gives 3. Passing keeps 3. Passing is at least as good, so best stays 3. The old best moves into before for the next house.Common pitfalls
Scanning the whole circle as one street
return rob_line(0, len(nums) - 1)
return max(rob_line(0, len(nums) - 2), rob_line(1, len(nums) - 1))
A straight scan does not know the ends touch. On [2, 3, 2] it robs both 2s for 4, but those two houses are neighbours in the circle. The answer is 3.
Leaving out only one end
return rob_line(0, len(nums) - 2)
return max(rob_line(0, len(nums) - 2), rob_line(1, len(nums) - 1))
Some best plans need the last house. On [1, 2, 3] the street without the last house gives 2, but robbing the 3 alone gives 3.
No special case for a single house
return max(rob_line(0, len(nums) - 2), rob_line(1, len(nums) - 1))
if len(nums) == 1:
return nums[0]With one house, both ranges are empty and both scans return 0. On [5] that answers 0 instead of 5.
Edge cases
[4, 7]They are neighbours both ways round. The streets are [4] and [7], so the answer is 7 without any extra check.
Complexity
nums[:-1] and nums[1:] would copy the list and cost O(n).House Robber II FAQ
Is the plan that robs neither end counted?
Yes, twice. It fits on both streets, so both scans can find it. Seeing it twice does no harm, because the answer is a maximum, not a count.
Can it be done in one pass?
Yes: run the street scan with an extra flag for whether house 0 was robbed, and refuse the last house when the flag is set. That is the same two cases carried side by side, still O(n). Two separate calls of one short function are easier to check.
What is House Robber III?
LeetCode 337 puts the houses on a binary tree, where a parent and child are neighbours. Each node returns two totals to its parent, the best with the node robbed and the best without, which is a different problem from the circle.