This is a follow up question to the House Robber problem on Leetcode. Please go through the explanation for the original problem here (https://nishantt.medium.com/house-robber-afe8131a5abb) before proceeding ahead.
The only change here is that the houses are now placed in a circular fashion i.e. the last house is the neighbor to the first house and vice versa. Hence the robber can now only rob either the last house or the first house in one go as robbing one would alert the other.
Recursion
Our approach here will be a modification of the original House Robber problem. So we are going to call our “Recurse” method here with different parameters. We know for a fact that the first and last house cannot be robbed together, so we process them individually by defining separate start and end to our houses we want to considered for robbing. We are going to call first Recurse(0,n-1) and Recurse(1,n) where n is the length of our houses array. In the end we are going to consider the loot which is maximum out of the two.
public class Solution {
public int Rob(int[] nums)
{
// Edge case handled as for a single house it robbing it will be the maximum loot possible.
if(nums.Length == 1 && nums[0] != 0) return nums[0];
return Math.Max(Recurse(nums,0,nums.Length-1),Recurse(nums,1,nums.Length));
}…