House robber II
The houses sit in a circle, so the first and last are neighbours too. Run house robber twice and keep the better result.
Watch it animatedStep through it, with the running line highlighted. Free, no sign-in needed to watch.The idea
In a circle, robbing both the first and the last house breaks the no-two-neighbours rule. So either the last house is off limits, or the first house is. Running the ordinary straight-line house robber on each of those two shortened rows and keeping the larger answer covers every safe option.
Code
public static int RobCircle(int[] a) {
if (a.Length == 1) return a[0];
int excludeLast = RobLinear(a, 0, a.Length - 2);
int excludeFirst = RobLinear(a, 1, a.Length - 1);
return Math.Max(excludeLast, excludeFirst);
}
static int RobLinear(int[] a, int lo, int hi) {
int prev = 0, cur = 0;
for (int i = lo; i <= hi; i++) {
int take = prev + a[i];
prev = cur;
cur = Math.Max(cur, take);
}
return cur;
}static int robCircle(int[] a) {
if (a.length == 1) return a[0];
int excludeLast = robLinear(a, 0, a.length - 2);
int excludeFirst = robLinear(a, 1, a.length - 1);
return Math.max(excludeLast, excludeFirst);
}
static int robLinear(int[] a, int lo, int hi) {
int prev = 0, cur = 0;
for (int i = lo; i <= hi; i++) {
int take = prev + a[i];
prev = cur;
cur = Math.max(cur, take);
}
return cur;
}int robLinear(const vector<int>& a, int lo, int hi);
int robCircle(const vector<int>& a) {
if (a.size() == 1) return a[0];
int excludeLast = robLinear(a, 0, (int)a.size() - 2);
int excludeFirst = robLinear(a, 1, (int)a.size() - 1);
return max(excludeLast, excludeFirst);
}
int robLinear(const vector<int>& a, int lo, int hi) {
int prev = 0, cur = 0;
for (int i = lo; i <= hi; i++) {
int take = prev + a[i];
prev = cur;
cur = max(cur, take);
}
return cur;
}def rob_circle(a: list[int]) -> int:
if len(a) == 1: return a[0]
exclude_last = rob_linear(a, 0, len(a) - 2)
exclude_first = rob_linear(a, 1, len(a) - 1)
return max(exclude_last, exclude_first)
def rob_linear(a: list[int], lo: int, hi: int) -> int:
prev = cur = 0
for i in range(lo, hi + 1):
take = prev + a[i]
prev = cur
cur = max(cur, take)
return curTips
- A circle of one house is a special case: there is no neighbour conflict, so just take it.
- Both sub-runs use the exact same straight-line recurrence as house robber; only the index range changes.
In the .NET library
It is tempting to just skip "the smaller of the two ends", but that is not always correct: only trying both full ranges and comparing their best totals is guaranteed to find the true optimum.