House Robber II (Circular)

MediumDynamic Programming1D DP

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A row of houses is arranged in a **circle**, so the first house and the last house are direct neighbors. Each house holds some amount of cash, given in an integer array nums where nums[i] is the money stashed in house i. You are planning a single night of break-ins, but there is a catch: the security systems of any two **adjacent** houses are linked, and robbing two neighboring houses on the same night triggers an alarm. Because the layout is circular, house 0 and house n - 1 count as adjacent in addition to the usual side-by-side pairs. Return the **maximum** total amount of money you can rob without ever hitting two adjacent houses. Since house 0 and house n - 1 can never both be robbed, the circular problem reduces to two independent linear sub-problems: rob from the houses nums[0 .. n-2] (skip the last) or from nums[1 .. n-1] (skip the first), and take the better result.

Examples

Input: nums = [2,3,2]
Output: 3
You cannot rob house 0 (money = 2) and house 2 (money = 2) together because they are adjacent in the circle. Robbing house 1 alone yields 3, which is the best possible.
Input: nums = [1,2,3,1]
Output: 4
Rob house 0 (money = 1) and house 2 (money = 3). They are not adjacent, and 1 + 3 = 4.
Input: nums = [5]
Output: 5
A single house has no neighbors to worry about, so you take all of its money.

Constraints

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

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.