House Robber II

Same as House Robber but houses are in a circle.

Pattern

Circular DP

This problem follows the Circular DP pattern, commonly found in the 1-D Dynamic Programming category. Recognizing this pattern is key to solving it efficiently in an interview setting.

Approach

How to Solve It

Run House Robber twice: once excluding first house, once excluding last. Take max.

Key Insight

Breaking the circle into two linear problems eliminates the circular constraint — at least one of the two ranges excludes both endpoints being robbed.

Step-by-step

  1. 1Since houses form a circle, house 0 and house n-1 can't both be robbed
  2. 2Run House Robber on nums[0..n-2] (exclude last)
  3. 3Run House Robber on nums[1..n-1] (exclude first)
  4. 4Return the maximum of the two results

Pseudocode

def rob_linear(houses):
    prev2, prev1 = 0, 0
    for h in houses:
        prev2, prev1 = prev1, max(prev1, prev2 + h)
    return prev1
return max(rob_linear(nums[:-1]), rob_linear(nums[1:]))
Complexity Analysis

Time Complexity

O(n)

Space Complexity

O(1)
More 1-D Dynamic Programming Problems

Master this pattern with YeetCode

Practice House Robber II and similar 1-D Dynamic Programming problems with flashcards. Build pattern recognition through active recall.

Practice this problem