213 House Robber IITotal Accepted:
24216 submissions:
80632 Difficulty:
Medium
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.
Subscribe to see which companies asked this question
This topic is based on house robber I, so I need to know the solution of houserobber I before doing this problem.
House robber I's Portal:
House robber I
If the first question has been AC, the meaning of this question is to change the house into a ring.
In the first question, the DP state transfer equation max[i] = Math.max (max[i-1], nums[i-1] + max[i-2]) has been made. Then the second question is very good.
In the second question, I'm going to do a DP again, and I need to record the first and last of the selected room, start and end.
When recording start, be aware of the start State: 1. When the front room has been selected. 2. When the front room is not selected.
So here's a summary of the DP transfer equation:
if(max[i-1] > nums[i-1] + max[i-2]) {max[i]= max[I-1 ]; start[i]= start[I-1 ]; } Else{max[i]= nums[I-1] + max[i-2 ]; Last=i; if(max[i-1] = = max[I-2]) {start[i]= start[I-2 ]; } Else{start[i]= start[I-2] = = 0? 2:start[i-2 ]; } }
Finally, if you choose the last one when the alarm, to determine whether to choose (1,n) or (0,n-1) the maximum value
The general idea is to carry out two DP, (1,n) and (0,n-1) respectively DP
Ps: There is no way to think of a better, more concise method. But I think there is, but I am stupid didn't think
Public classSolution { Public intRobint[] nums) { int[] max =New int[Nums.length + 1 ]; int[] start =New int[Nums.length + 1 ]; max[0] = 0; start[0] = 0; if(Nums = =NULL|| Nums.length = = 0 ) return0; max[1] = nums[0 ]; start[1] = 1; intLast = 1; for(inti = 2; I <= nums.length; i++ ) { if(max[i-1] > nums[i-1] + max[i-2]) {max[i]= max[I-1 ]; start[i]= start[I-1 ]; } Else{max[i]= nums[I-1] + max[i-2 ]; Last=i; if(max[i-1] = = max[I-2]) {start[i]= start[I-2 ]; } Else{start[i]= start[I-2] = = 0? 2:start[i-2 ]; } } } if(last + 1)% Nums.length = =start[Nums.length]) { int[] tail =New int[Nums.length-1]; System.arraycopy (Nums,1, tail, 0, nums.length-1 ); intPremax =rob2 (tail); max[Nums.length]= Math.max ((max[nums.length]-max[1]), max[nums.length-1 ] ); max[Nums.length]=Math.max (max[nums.length], Premax); } returnmax[Nums.length]; } Public intROB2 (int[] nums) { int[] max =New int[Nums.length + 1 ]; max[0] = 0; if(Nums = =NULL|| Nums.length = = 0 ) return0; max[1] = nums[0 ]; for(inti = 2; I <= nums.length; i++) {max[i]= Math.max (max[i-1], nums[i-1] + max[i-2 ] ); } returnmax[Nums.length]; } Public Static voidMain (string[] args) {solution S=Newsolution (); int[] Nums =New int[] {2, 2, 4, 3, 2, 5 }; System.out.println (S.rob (nums)); }}
[Leetcode] House robber II