</>

213. House Robber II

Medium NeetCode 150
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
1-D Dynamic Programming
Time
—
Space
—

Standard Solution (C++)

SummaryGiven a circular array of house values where the first and last houses are neighbors, find the maximum amount you can rob without taking from two adjacent houses. Because the houses form a circle, you cannot rob both the first and last house.

Approach
  • The circular constraint means the first and last houses cannot both be robbed, so split the problem into two independent linear House Robber subproblems: one over nums[0..n-2] (include first, exclude last) and one over nums[1..n-1] (exclude first, include last).
  • For each subproblem, use the standard O(1)-space DP: track only the two previous best values (prev2 and prev1) and update them as you scan left to right.
  • The answer is the maximum of the two subproblem results.
  • Edge case: a single house must be handled separately since the two ranges would otherwise overlap incorrectly.
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
    // Linear House Robber on nums[lo..hi] (inclusive), O(n) time, O(1) space.
    int robRange(const vector<int>& nums, int lo, int hi) {
        int prev2 = 0, prev1 = 0;
        for (int i = lo; i <= hi; ++i) {
            int cur = max(prev1, prev2 + nums[i]);
            prev2 = prev1;
            prev1 = cur;
        }
        return prev1;
    }

public:
    int rob(vector<int>& nums) {
        int n = nums.size();
        if (n == 1) return nums[0];

        // Case 1: rob from house 0 to n-2 (first house eligible, last excluded)
        // Case 2: rob from house 1 to n-1 (first house excluded, last eligible)
        return max(robRange(nums, 0, n - 2),
                   robRange(nums, 1, n - 1));
    }
};
Time O(n) Space O(1)

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

#include <vector>
using namespace std;

class Solution {
public:
    int rob(vector<int>& nums) {
        // 1. include nums[0]: find max in nums[2, ..., n-2]
        // 2. not include nums[0]: find max in nums[1, ..., n-1]

        int n=nums.size();
        int prev1=0;
        int prev2=0;

        for(int i=2; i<n-1; ++i){
            int cur=std::max(prev1, prev2+nums[i]);
            prev2=prev1;
            prev1=cur;
        }
        int max1=prev1+nums[0]; // nums[0] is 0 or positive

        prev1=0;
        prev2=0;
        
        for(int i=1; i<n; ++i){
            int cur=std::max(prev1, prev2+nums[i]);
            prev2=prev1;
            prev1=cur;
        }
        int max2=prev1;
        
        return std::max(max1, max2);
    }
};

// Time: O(N)
// Space: O(1)

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern