In this Leetcode House Robber II problem solution, You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.

Leetcode House Robber II problem solution


Problem solution in Python.

class Solution:
    def rob_help(self, nums):
            n = len(nums)
            if n < 2:
                return 0 if not n else nums[0]

            prev = 0
            curr = nums[0]
            for i in range(1, n):
                tmp = max(prev + nums[i], curr)
                prev, curr = curr, tmp

            return curr

    def rob(self, nums):
        n = len(nums)
        if n < 2:
            return 0 if not n else nums[0]

        nums = [(c, 0) for c in nums]
        nums[0] = (nums[0][0], 1)
        nums[-1] = (nums[-1][0], 1)

        prev = (0, False)
        curr = nums[0]
        for i in range(1, n):
            tmp = curr
            if prev[0] + nums[i][0] > curr[0]:
                curr = (prev[0] + nums[i][0], prev[1] + nums[i][1])
            prev = tmp  
        if curr[1] == 2:
            return max(self.rob_help([c[0] for c in nums[1:]]), self.rob_help([c[0] for c in nums[:-1]]))
        else:
            return curr[0]



Problem solution in Java.

public class Solution {

public int rob(int[] nums) {
    if(nums.length==1) return nums[0];
    int rob=0,notrob=0;
    for(int i=0;i<nums.length-1;i++){
        int currob=notrob+nums[i];
        notrob=Math.max(rob,notrob);
        rob=currob;
    }
    int firstcase=Math.max(rob,notrob);
    rob=0;notrob=0;
    for(int i=1;i<nums.length;i++){
        int currob=notrob+nums[i];
        notrob=Math.max(rob,notrob);
        rob=currob;
    }
    int secondcase=Math.max(rob,notrob);
    return Math.max(firstcase,secondcase);
}
}


Problem solution in C++.

int rob(vector<int>& v) 
    {
        if(v.size()==1) return v[0];
        if(v.size()==2) return max(v[1],v[0]);
        if(v.size()==3) return max(v[0],max(v[1],v[2]));
        int n=v.size();
        int a[n],i;
        memset(a,0,sizeof(a));
        a[0]=v[0];
        a[1]=max(a[0],v[1]);
        for(i=2;i<n-1;i++)
            a[i]=max(a[i-1],v[i]+a[i-2]);
        
        int l=a[n-2];
        memset(a,0,sizeof(a));
        a[1]=v[1];
        a[2]=max(a[1],v[2]);
        
        for(i=3;i<n;i++)
            a[i]=max(a[i-1],a[i-2]+v[i]);
        
        return max(l,a[n-1]);
        
    }


Problem solution in C.

int max(int a,int b){
    return a>b?a:b;
}

int rob(int* nums, int n){
    if(n==1) return nums[0];
    int dp[n-1];
    int arr[n];
    for(int i=0;i<n-1;i++) dp[i]=nums[i];
    arr[0]=0;
    for(int i=1;i<n;i++) arr[i]=nums[i];
    for(int i=1;i<n-1;i++){
        for(int j=0;j<i;j++){
            if(j+1!=i) dp[i]=max(dp[i],dp[j]+nums[i]); // excluding the last house
        }
    }
    for(int i=2;i<n;i++){
        for(int j=1;j<i;j++){
            if(j+1!=i) arr[i]=max(arr[i],arr[j]+nums[i]); // excluding the first house
        }
    }
    int max_ans=INT_MIN;
    for(int i=0;i<n-1;i++){
        int temp=dp[i]>arr[i+1]?dp[i]:arr[i+1];
        if(temp > max_ans) max_ans=temp;
    }
    return max_ans;
}