House robber II
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.
The difference with House robber is that nums[0] and nums[n-1] cannot be included,
Thus the equivalent of nums[0...n-2] and nums[1...n-1] takes a larger value between the two.
classSolution { Public: intRob (vector<int>&nums) { if(Nums.empty ())return 0; if(nums.size () = =1) returnnums[0]; Vector<int>nums1 (nums); Vector<int>nums2 (nums); Nums1.erase (Nums1.begin ()); Nums2.pop_back (); returnMax (Originrob (NUMS1), Originrob (NUMS2)); } intOriginrob (vector<int>&nums) { if(Nums.empty ())return 0; intPrev2 =0; intPREV1 = nums[0]; for(inti =1; I < nums.size (); i + +) { intcur = max (prev2+Nums[i], PREV1); Prev2=Prev1; Prev1=cur; } returnPrev1; }};
"Leetcode" 213. House robber II