PROBLEM 49 OF 75

House Robber II

Maximize non-adjacent values arranged in a circle.

PATTERNDynamic programmingState is the smallest information needed to finish.
SPOT THE SIGNAL

Why does this pattern fit?

FRAME · 1 OF 4

Restate the exact job

Maximize non-adjacent values arranged in a circle.

WHY THIS FITS

First and last cannot both be used, so split into two linear cases.

COMPLEXITY

O(n) time · O(1) space

COMMON FAILURE

Running the linear recurrence across the full circle permits both endpoints.

THE REASONING, IN ONE PLACE

How to solve House Robber II

The goal is to solve this problem from the pattern, not to memorize a finished answer. Use this as a check after your own attempt.

What the question asks

Maximize non-adjacent values arranged in a circle.

Why Dynamic programming fits

First and last cannot both be used, so split into two linear cases.

State to maintain

Linear robber state for ranges excluding first or excluding last.

Transition

Solve both ranges and take the larger; handle one house separately.

Time and space

O(n) time · O(1) space

Counterexample to the tempting mistake

Running the linear recurrence across the full circle permits both endpoints.

Prove it again tomorrow

Close this page. Rebuild the state and transition from memory, write a test that exposes the mistake above, then solve a fresh input without looking back. A same-day reread is practice, not proof of retention.

Open blank recall ↗