Blind 75 · 1-D Dynamic Programming

House Robber II

Medium

Watch the walkthrough (6:35)

Problem

Same street, same alarm — but now the houses stand in a circle, so the first and last are neighbours too.
Given nums where nums[i] is the money in house i, return the most you can take without robbing two adjacent houses.

Examples

Example 1:
Input: nums = [2,3,2]
Output: 3
Explanation: houses 0 and 2 are adjacent on the circle, so 2+2 is illegal.

Example 2:
Input: nums = [1,2,3,1]
Output: 4

Example 3:
Input: nums = [1]
Output: 1

Example 4:
Input: nums = [200,3,140,20,10]
Output: 340
Explanation: houses 0 and 2; house 4 would close the circle onto house 0.

Constraints

1 <= nums.length <= 100
0 <= nums[i] <= 1000

Prerequisites

Dynamic programming — being able to say, in one sentence, what a single cell of your table MEANS. Everything else follows from that sentence.
Rolling variables: when a state only looks back one or two steps, the table collapses to a couple of numbers.

How to think about it

1. Skip the First, Then the Last Optimal

Intuition

On the circle the first house and the last are neighbours, so no plan robs both: every plan leaves out the first house or the last. Leaving one out opens the circle into a straight street, which House Robber solves with two rolling bests. So solve the street without the first house and the street without the last, and keep the better. A circle of one house has no street on either side, so that house is its own answer.

Algorithm

1. One house: return it.
2. skip_first = rob_street(nums, 1, len(nums)): the street without the first house.
3. skip_last = rob_street(nums, 0, len(nums) - 1): the street without the last house.
4. On a street, keep two bests, two_back and one_back, both 0. At each house, best = max(one_back, two_back + nums[i]): skip it, or rob it on top of the best from two back. Then two_back = one_back and one_back = best. The street's answer is one_back.
5. Return max(skip_first, skip_last).

Time & Space

Time O(n): two streets, each a single walk. Space O(1): two numbers a walk, however many houses there are.

2. Two Streets, One Rule Optimal

Intuition

The circle adds exactly one constraint: the first and last houses cannot both be robbed. So drop one of them and the problem is the straight street again. Run the previous solution twice — once without the last house, once without the first — and take the better. A single house is its own answer, since both slices would be empty.

Algorithm

1. One house: return it.
2. Solve the straight-street problem on houses 0..n-2.
3. Solve it again on houses 1..n-1.
4. Return the larger.

Time & Space

Time O(n) — two linear passes. Space O(1).