LeetCode #213 Medium

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.

  • nums can hold a single house, which you may rob.
Constraints
  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 1000
dynamic-programmingarray
Open on LeetCode ↗
02

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.

How to spot this pattern

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.

03

Approach

Try it first

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.

1

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.

2

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.

3

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.

4

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.

5

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.

04

Code in Python, C++ and Java

▶1from typing import List
▶2 
▶3 
▶4class Solution:
▶5 def rob(self, nums: List[int]) -> int:
▶6 if len(nums) == 1:
▶7 return nums[0]
▶8 
▶9 def rob_line(lo: int, hi: int) -> int:
▶10 before, best = 0, 0
▶11 for i in range(lo, hi + 1):
▶12 before, best = best, max(best, before + nums[i])
▶13 return best
▶14 
▶15 return max(rob_line(0, len(nums) - 2), rob_line(1, len(nums) - 1))
houses in a circle2#03#12#2without the last housebeforebestwithout the first housebeforebesthousemax,+=pass itrob ithouse 0 and house 2 are neighbours too
houses3
The red arc is the link a straight street does not have: the first and last houses touch. Any allowed plan leaves out at least one of them, so the search splits into two straight streets.
houses in a circle2#03#12outwithout the last house0before0bestwithout the first housebeforebesthousemax,+=pass itrob itstreet 1: leave out house 2
rangehouses 0 to 1
before0
best0
Street 1 covers every plan that does not rob house 2. With that house gone, the circle is cut and houses 0 to 1 form an ordinary line, so the arc no longer matters.
houses in a circle2#03#12outwithout the last house0before2bestwithout the first housebeforebesthouse 0max0,0+2=2pass itrob ithouse 0: rob it → best 2
money2
before0
best2
Robbing house 0 adds its 2 to 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.
houses in a circle2#03#12outwithout the last house2before3bestwithout the first housebeforebesthouse 1max2,0+3=3pass itrob ithouse 1: rob it → best 3
money3
before2
best3
Robbing house 1 adds its 3 to 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.
houses in a circle2out3#12#2without the last house2before3bestwithout the first house0before0besthousemax,+=pass itrob itstreet 2: leave out house 0
rangehouses 1 to 2
before0
best0
Street 2 covers every plan that does not rob house 0. With that house gone, the circle is cut and houses 1 to 2 form an ordinary line, so the arc no longer matters.
houses in a circle2out3#12#2without the last house2before3bestwithout the first house0before3besthouse 1max0,0+3=3pass itrob ithouse 1: rob it → best 3
money3
before0
best3
Robbing house 1 adds its 3 to 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.
houses in a circle2out3#12#2without the last house2before3bestwithout the first house3before3besthouse 2max3,0+2=3pass itrob ithouse 2: pass it, best stays 3
money2
before3
best3
Robbing house 2 adds its 2 to 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.
houses in a circle2#03#12outwithout the last house2before3bestwithout the first house3before3bestanswermax3,3=3answer: 3 from house 1
street 13
street 23
answer3
Street 1 gave 3 and street 2 gave 3. Every allowed plan lives on at least one street, so the larger, 3, is the answer: rob house 1, which never uses both ends.
05

Common pitfalls

Scanning the whole circle as one street

✗ Wrong
return rob_line(0, len(nums) - 1)
✓ Right
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

✗ Wrong
return rob_line(0, len(nums) - 2)
✓ Right
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

✗ Wrong
return max(rob_line(0, len(nums) - 2), rob_line(1, len(nums) - 1))
✓ Right
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.

06

Edge cases

Two houses, e.g. [4, 7]

They are neighbours both ways round. The streets are [4] and [7], so the answer is 7 without any extra check.

07

Complexity

Time
O(n)
Space
O(1)
Two passes of about n houses each. Scanning by index range keeps the extra space to a few integers; slicing nums[:-1] and nums[1:] would copy the list and cost O(n).
08

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.