After robbing those houses on that street, the thief have found himself a new place for his thievery so that he would not GE T too much attention. This time, all houses at the is arranged in a circle. That's means the first house was the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.
Given a list of non-negative integers representing the amount of money in each house, determine the maximum amount of mone Y you can rob tonight without alerting the police.
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
1 Public classSolution {2 Public intRobint[] nums) {3 if(Nums = =NULL|| Nums.length <1)return0;4 if(Nums.length = = 1)returnNums[0];5 if(Nums.length = = 2)returnMath.max (nums[0],nums[1]);//divide it into two situations: 1. Including Nums[0] 2. including nums[nums.length-1]6 returnMath.max (Getrob (nums,0,nums.length-2), Getrob (nums,1,nums.length-1));7 }8 9 Public intGetrob (int[] Nums,intSinte) {Ten intLen = e-s + 1; One int[] res =New int[Len]; ARes[0] =Nums[s]; -RES[1] = Math.max (nums[s],nums[s+1]); - for(inti = 2;i<len;i++){ theRes[i] = Math.max (Res[i-2]+nums[s+i], res[i-1]); - } - returnRes[len-1]; - } +}
213. House robber II Java Solutions