Note:this is a extension of house robber.
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.
Runtime:0ms
1 classSolution {2 Public:3 intRob (vector<int>&nums) {4 intn =nums.size ();5 if(n = =0)return 0;6 if(n = =1)returnnums[0];7 if(n = =2)returnMax (nums[0], nums[1]);8 9 int*DP1 =New int[n];Tendp1[0] = nums[0]; Onedp1[1] = nums[0]; A //Rub the first house - for(inti =2; I < n-1; i++){ -Dp1[i] = max (Dp1[i-2] + nums[i], dp1[i-1]); the } -Dp1[n-1] = Dp1[n-2]; - - //Don't rub the first house + int*DP2 =New int[n]; -dp2[0] =0; +dp2[1] = nums[1]; A for(inti =2; I < n; i++){ atDp2[i] = max (Dp2[i-2] + nums[i], dp2[i-1]); - } - - returnMax (Dp1[n-1], Dp2[n-1]); - } -};
House robber II