Try it first — give it 25 minutes
0/4 revealedStuck? Take one hint at a time. The struggle is where the pattern sticks.
The problem in plain English
This is House Robber with one twist. nums[i] is still the money in house i, and you still can't rob two neighbours. But now the houses form a circle: house 0 and house n − 1 are neighbours as well. Return the largest total you can take.
There are 1 to 100 houses, each holding 0 to 1000.
Example: nums = [2, 3, 2] → 3. On a straight street you'd rob both 2s for 4, but they're neighbours in the circle, so the best is the single 3.
Start with brute force
Try every set of houses, reject any set with two neighbours (counting the pair 0 and n − 1), and keep the best total. That's 2ⁿ sets, about 10³⁰ for n = 100.
You could also try to bolt a "did I rob house 0?" flag onto the old DP. It works, but there's a simpler idea that reuses what you already know.
Building the intuition
Start from what you know. In House Robber, dp[i] was the best total from the first i houses, and dp[i] = max(dp[i − 1], dp[i − 2] + money). That works on any straight line of houses.
What does the circle break? Only one thing: on a straight line the first and last houses aren't neighbours, so a plan could rob both. In the circle it can't.
Now think about any valid plan in the circle. The first and last houses can't both be robbed. So at least one of them is left alone:
- Case A: the last house is left alone. Then you might as well delete it. The rest, houses
0 … n − 2, is a straight line. Its first and last houses aren't neighbours any more. - Case B: the first house is left alone. Delete it. Houses
1 … n − 1are a straight line.
Every valid circle plan fits in A or B (or both), and every straight-line plan for A or B is a valid circle plan. So the answer is max(best(A), best(B)). Two straight-line problems, and you already have the solution.
The recipe for each run is the same as before:
- State.
dp[i]means the most money from the firstihouses of this range. - Recurrence.
dp[i] = max(dp[i − 1], dp[i − 2] + money): skip this house, or rob it. - Base cases.
dp[0] = 0, and the "two back" value starts at 0. - Order. Left to right. Each cell reads only the two before it, so keep two variables,
prev2 = dp[i − 2]andprev1 = dp[i − 1], instead of a table. That's the space optimisation from House Robber.
The optimal approach
- If there's one house, return
nums[0]. (Both ranges below would be empty.) - Write
robLine(lo, hi): plain House Robber onnums[lo..hi].- Start with
prev2 = 0,prev1 = 0. - For each house
kfromlotohi:cur = max(prev1, prev2 + nums[k]), then shiftprev2 = prev1,prev1 = cur. - Return
prev1.
- Start with
- Return
max(robLine(0, n − 2), robLine(1, n − 1)).
The subtle detail: the single house. With n = 1 the ranges are 0..−1 and 1..0, both empty, so both runs return 0. But one house has no neighbour at all, and you should rob it. That's why step 1 exists.
The code
Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.
class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) == 1:
return nums[0] # one house has no neighbours, not even itself
# The first and last houses touch, so at most one of them is robbed.
# Solve the straight-line problem twice: without the last house, and without the first.
return max(self.robLine(nums, 0, len(nums) - 2),
self.robLine(nums, 1, len(nums) - 1))
def robLine(self, nums: List[int], lo: int, hi: int) -> int:
# House Robber on nums[lo..hi] with the table squeezed into two variables:
# prev2 = dp[i-2], prev1 = dp[i-1], where dp[i] = best from the first i houses of the range
prev2, prev1 = 0, 0
for k in range(lo, hi + 1):
cur = max(prev1, prev2 + nums[k]) # skip house k, or rob it on top of dp[i-2]
prev2, prev1 = prev1, cur
return prev1class Solution {
public:
int rob(vector<int>& nums) {
int n = nums.size();
if (n == 1) return nums[0]; // one house has no neighbours, not even itself
// The first and last houses touch, so at most one of them is robbed.
// Solve the straight-line problem twice: without the last house, and without the first.
return max(robLine(nums, 0, n - 2), robLine(nums, 1, n - 1));
}
private:
int robLine(vector<int>& nums, int lo, int hi) {
// House Robber on nums[lo..hi] with the table squeezed into two variables:
// prev2 = dp[i-2], prev1 = dp[i-1], where dp[i] = best from the first i houses of the range
int prev2 = 0, prev1 = 0;
for (int k = lo; k <= hi; k++) {
int cur = max(prev1, prev2 + nums[k]); // skip house k, or rob it on top of dp[i-2]
prev2 = prev1;
prev1 = cur;
}
return prev1;
}
};class Solution {
public int rob(int[] nums) {
int n = nums.length;
if (n == 1) return nums[0]; // one house has no neighbours, not even itself
// The first and last houses touch, so at most one of them is robbed.
// Solve the straight-line problem twice: without the last house, and without the first.
return Math.max(robLine(nums, 0, n - 2), robLine(nums, 1, n - 1));
}
private int robLine(int[] nums, int lo, int hi) {
// House Robber on nums[lo..hi] with the table squeezed into two variables:
// prev2 = dp[i-2], prev1 = dp[i-1], where dp[i] = best from the first i houses of the range
int prev2 = 0, prev1 = 0;
for (int k = lo; k <= hi; k++) {
int cur = Math.max(prev1, prev2 + nums[k]); // skip house k, or rob it on top of dp[i-2]
prev2 = prev1;
prev1 = cur;
}
return prev1;
}
}/**
* @param {number[]} nums
* @return {number}
*/
var rob = function (nums) {
const n = nums.length;
if (n === 1) return nums[0]; // one house has no neighbours, not even itself
// The first and last houses touch, so at most one of them is robbed.
// Solve the straight-line problem twice: without the last house, and without the first.
return Math.max(robLine(nums, 0, n - 2), robLine(nums, 1, n - 1));
};
// House Robber on nums[lo..hi] with the table squeezed into two variables:
// prev2 = dp[i-2], prev1 = dp[i-1], where dp[i] = best from the first i houses of the range
function robLine(nums, lo, hi) {
let prev2 = 0, prev1 = 0;
for (let k = lo; k <= hi; k++) {
const cur = Math.max(prev1, prev2 + nums[k]); // skip house k, or rob it on top of dp[i-2]
prev2 = prev1;
prev1 = cur;
}
return prev1;
}Dry run
nums = [200, 3, 140, 20, 10]. On a straight street the best would be 200 + 140 + 10 = 350, but that robs both house 0 and house 4, which touch in the circle.
Run A: houses 0 to 3 ([200, 3, 140, 20], last house left out)
| k | nums[k] | prev2 = dp[i − 2] | prev1 = dp[i − 1] | cur = max(prev1, prev2 + nums[k]) |
|---|---|---|---|---|
| 0 | 200 | 0 | 0 | max(0, 200) = 200 |
| 1 | 3 | 0 | 200 | max(200, 3) = 200 |
| 2 | 140 | 200 | 200 | max(200, 340) = 340 |
| 3 | 20 | 200 | 340 | max(340, 220) = 340 |
The cur column is the DP table for run A: dp = [0, 200, 200, 340, 340]. Best: 340.
Run B: houses 1 to 4 ([3, 140, 20, 10], first house left out)
| k | nums[k] | prev2 = dp[i − 2] | prev1 = dp[i − 1] | cur = max(prev1, prev2 + nums[k]) |
|---|---|---|---|---|
| 1 | 3 | 0 | 0 | max(0, 3) = 3 |
| 2 | 140 | 0 | 3 | max(3, 140) = 140 |
| 3 | 20 | 3 | 140 | max(140, 23) = 140 |
| 4 | 10 | 140 | 140 | max(140, 150) = 150 |
Table for run B: dp = [0, 3, 140, 140, 150]. Best: 150.
Answer: max(340, 150) = 340 (houses 0 and 2).
Complexity
- Time: O(n). Two linear passes.
- Space: O(1). Each pass keeps two variables. (With full tables it would be O(n).)
Edge cases and traps
- One house. Return it directly. Without the special case you return 0.
- Two houses. They're neighbours, so the answer is the larger one. The two runs each see a single house and handle this correctly.
- Running House Robber once on the whole array. It may rob both ends, as in the dry run (350 instead of 340).
- Excluding both ends in one run.
robLine(1, n − 2)misses plans that rob house 0 or housen − 1. You need two separate runs, each excluding only one end. - Updating
prev2andprev1in the wrong order. Computecurfirst, then shift. Overwritingprev1before using it losesdp[i − 1].
Say it in the interview
"The circle only adds one rule: the first and last houses can't both be robbed. So any valid plan skips at least one of them. I solve the straight-line House Robber twice, once on houses 0 to n − 2 and once on 1 to n − 1, and take the max. Each run is the two-variable DP,
cur = max(prev1, prev2 + nums[k]). That's O(n) time and O(1) space. One house is a special case: I just return it."
Follow-ups interviewers ask
- "Why is it safe to delete a house instead of forcing it to be skipped?" Skipping a house and deleting it give the same set of choices for everyone else, and deleting it also removes its edge to the far end.
- "What if the houses form a tree instead?" That's House Robber III: for each node, compute two values (best if you rob it, best if you don't) from its children, in a post-order DFS.
- "Can you do it in one pass?" Yes, by running both two-variable DPs side by side in the same loop. Same complexity; it just saves a second loop.
Practise the same idea
This is Day 81 of a free 90-day plan 🐸
90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.
Get the full roadmap — free