Skip to content
AI360Xpert
Beta
Difficulty: Medium1-D Dynamic Programming

House Robber II

Problem in Plain English

Given cash in houses arranged in a circle, rob the largest total possible where both adjacent houses and the first-last pair cannot be robbed together.

Problem Statement

You are given an integer array nums where nums[i] is the money in house i, and the houses form a circle: house 0 and house n - 1 are adjacent. Robbing two adjacent houses triggers the alarm.

Return the maximum money robbable with no two robbed houses adjacent around the circle. With a single house, its value is the answer.

Constraints

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 1000

Examples

Example 1

Input
nums = [2, 3, 2]
Output
3

Houses 1 and 3 are adjacent around the circle, so rob house 2 alone for 3.

Example 2

Input
nums = [1, 2, 3, 1]
Output
4

Rob house 1 (money 1) and house 3 (money 3) for a total of 4.

Example 3

Input
nums = [1, 2, 3]
Output
3

Robbing house 3 alone for 3 beats every pair, since houses 1 and 3 are now neighbours.

Example 4

Input
nums = [7]
Output
7

A single house is not a conflicting pair with itself. Rob it for 7; removing either endpoint would incorrectly leave an empty line.

Intuition

The circle adds one conflict: the first and last houses are neighbours. In Example 1, [2, 3, 2], taking both endpoint values would give 4 but trigger the alarm. Every valid plan leaves at least one endpoint unrobbed. Solve the line without the last house and the line without the first, then keep the richer answer. Within either line, each house is a choice between skipping it and taking it with the best plan that ends two houses earlier.

Approaches

Recursive Take Or Skip

Solution Details

Reveal Recursive Take Or Skip: intuition, complexity, and code

Hints

Hint 1
The circle has exactly one extra rule beyond the street version: houses 0 and n - 1 are neighbours.
Hint 2
That rule forbids a single pair, so decide the fate of house 0 first and solve the remaining line both ways.
Hint 3
Run the linear robber on 0..n-2 and on 1..n-1, then keep the richer run.

Edge Cases

  • One house has no neighbour conflict, so its value is the answer and both ranges would be empty.
  • Two houses return the richer one, since each run covers exactly one house.
  • All zeros return 0 from either run without special handling.

Common Mistakes and Interview Tips

  • Running the linear solver on the full circle double-counts the forbidden pair on inputs like [2, 3, 2].
  • Forgetting the single-house guard makes both ranges empty and returns 0 instead of the house value.
  • Solving the same shortened line twice misses plans that require the excluded endpoint. For [1, 2, 3, 9], dropping the last house twice gives only 4 instead of the correct 11 from indices 1 and 3.
  • Do not add the two range answers: they are competing complete plans and may share houses. In an interview, justify both coverage of every circular plan and validity of each shortened line.

Key Takeaway

Break a circular adjacency ban by branching on one member of the wrapped pair; each branch is a line problem solvable with the same rolling recurrence.