House Robber II
Difficulty: Medium
Problem
Same street, one twist: the houses now form a CIRCLE — the first and last house are neighbors, so robbing both trips the alarm. Maximize loot under the same no-adjacent rule. The lesson: reduce a new problem to two runs of one you've already solved.
Example
Input: nums = [2,3,2]
Output: 3
Explanation: Houses 0 and 2 are now adjacent (circle!), so 2+2 is illegal. The best single choice is the middle house: 3.
Brute-force approach
No new naive phase — the linear robber's full arc was last lesson. This lesson is about the reduction.
Optimal approach
Key insight: The first and last house can never both be robbed. That single fact dissolves the circle into two straight lines — both already solved by yesterday's algorithm.
Split on the one new conflict: solve the line without the last house and the line without the first house with the linear robber, then take the better result.
Steps
- Guard: one house → return its loot
- Case A: linear rob on nums[0..n−2] (last house excluded)
- Case B: linear rob on nums[1..n−1] (first house excluded)
- Answer: max(A, B)
Time complexity: O(n) · Space complexity: O(1) (plus the slices; index bounds avoid even those)