Skip to content

House Robber II

Rob houses arranged in a circle for the most money, never taking two neighbors.

Medium1D Dynamic Programming
Solve on LeetCode

The problem

Houses stand in a circle, so the first and the last house are neighbors, and nums[i] is the money in house i. Robbing two neighboring houses sets off an alarm. Return the most money you can take without robbing any two neighbors.

Input nums = [6, 2, 3, 7]

Output 9

In a row you would take 6 and 7, but in a circle they are neighbors. The best is 6 + 3 or 2 + 7.

Input nums = [5, 2, 4, 6]

Output 9

Rob 5 and 4. The pair 5 and 6 would be worth more, but those two houses touch.

Key constraint: The only new rule compared to House Robber is that house 0 and house n - 1 can't both be robbed.

How to spot the pattern

  • It is House Robber, but the first and last items also count as neighbors.
  • A circle can often be broken into a couple of straight lines.

The idea

In any valid plan, at least one of the first and last houses stays untouched. So the answer is the better of two plain House Robber runs: one on houses 0 to n - 2, which leaves out the last house, and one on houses 1 to n - 1, which leaves out the first. Each run uses the usual state best(i) with best(i) = max(best(i - 1), best(i - 2) + nums[i]).

Brute force: Try every set of houses and reject the ones with neighbors, counting the first and last as neighbors, which takes O(2^n) time.

Step by step

  1. 1If there is only one house, return its money.
  2. 2Run House Robber on houses 0 to n - 2, keeping two running totals and computing here = max(oneBack, twoBack + nums[i]) at each house.
  3. 3Run it again on houses 1 to n - 1.
  4. 4Return the larger of the two results.

See the pattern move

The 1d dynamic programming pattern on a small example. Step through it, or press play.

Code

class Solution:
    def rob(self, nums: List[int]) -> int:
        if len(nums) == 1:
            return nums[0]
        # The first and last houses are neighbors, so at most one of them is robbed:
        # solve the street without the last house, then without the first, and keep the better.
        return max(self.rob_line(nums, 0, len(nums) - 2), self.rob_line(nums, 1, len(nums) - 1))

    def rob_line(self, nums: List[int], lo: int, hi: int) -> int:
        # House Robber on nums[lo..hi]: best(i) = max(best(i - 1), best(i - 2) + nums[i])
        two_back, one_back = 0, 0
        for i in range(lo, hi + 1):
            two_back, one_back = one_back, max(one_back, two_back + nums[i])
        return one_back

Complexity

Time

O(n)

Space

O(1)

Two passes over the houses, each with two running totals.

Watch out for

  • Forgetting the single-house case: both ranges are empty then, and the answer would come out as 0 instead of nums[0].
  • Leaving out both the first and the last house in one run, which misses every plan that uses one of them.

Follow-up questions

Why not run House Robber once and check whether it used both ends?

When it did, you still don't know which end to drop without solving both cases. Two runs cover every plan directly.

What if the houses formed a binary tree, with parent and child as neighbors?

Run a DFS that returns two values per node: the best with that node robbed and the best without it. That is House Robber III.

In Hinglish

Circle mein pehla aur aakhri ghar padosi hain. Ek baar pehla ghar hata ke House Robber chalao, ek baar aakhri hata ke, jo zyada aaye wahi jawab.

Related problems