House Robber II
Medium · Dynamic programming
Jump to codeHouses form a circle and have nonnegative amounts. Maximize the sum of chosen houses without choosing any neighbors, including the first and last together.
Examples
Example 1
nums = [2,3,2]
Output: 3
Example 2
nums = [1,2,3,1]
Output: 4
Constraints
- 1 <= nums.length <= 100
- 0 <= nums[i] <= 100
Need a hint?
Every valid answer excludes either the first house or the last.
Real dynamic programming interview questions
Questions on the same topic that candidates reported from real company interviews. Some company questions are locked and open with Premium or XP.
- Minimize Branch Merge ConflictsAmazon
- Count Distinct Hat Assignments and Maximize Hat PointsRoblox
- Find and Count Maximum-Score Grid PathsOpenAI
Solution approach
Handle empty and single-house inputs directly. Run the linear house-robber recurrence on indices 0 through n−2 and separately on 1 through n−1. Return the larger result. Iterate over index ranges to keep auxiliary space constant.
Complexity
O(n) time and O(1) extra space.
Python reference solution
def rob(nums):
if len(nums) <= 1:
return nums[0] if nums else 0
def line(start, end):
before = current = 0
for i in range(start, end):
before, current = current, max(current, before + nums[i])
return current
return max(line(0, len(nums) - 1), line(1, len(nums)))
JavaScript solution
function rob(nums) {
if (nums.length <= 1) return nums[0] || 0;
function line(start, end) {
let before = 0, current = 0;
for (let i = start; i < end; i++) {
[before, current] = [current, Math.max(current, before + nums[i])];
}
return current;
}
return Math.max(line(0, nums.length - 1), line(1, nums.length));
}
Java solution
import java.util.*;
class Solution {
public int rob(int[] nums) {
if (nums.length <= 1) return nums.length == 0 ? 0 : nums[0];
return Math.max(line(nums, 0, nums.length - 1), line(nums, 1, nums.length));
}
private int line(int[] nums, int start, int end) {
int before = 0, current = 0;
for (int i = start; i < end; i++) {
int next = Math.max(current, before + nums[i]);
before = current; current = next;
}
return current;
}
}
C++ solution
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int rob(vector<int> &nums) {
if (nums.size() <= 1) return nums.empty() ? 0 : nums[0];
auto line = [&](int start, int end) {
int before = 0, current = 0;
for (int i = start; i < end; i++) {
int next = max(current, before + nums[i]);
before = current; current = next;
}
return current;
};
return max(line(0, nums.size() - 1), line(1, nums.size()));
}
};
Trace a boundary case, then implement the approach in the editor. Submit checks your solution against all 9 test cases.
Keep your reasoning, pseudocode, and edge cases here.
Notes stay in this browser when storage is available.
Accepted submissions mark this problem complete. Progress stays in this browser.
[
[
2,
3,
2
]
]3
[
[
1,
2,
3,
1
]
]4
[
[
1,
2,
3
]
]3
[
[
1
]
]1
[
[
2,
3,
2,
3
]
]6
Drafts are saved per language in this browser when storage is available.