House Robbery I topic
Suppose you are a professional thief, ready to rob a house along a street. Each house is stocked with a certain amount of money. The only constraint you face is that the neighboring house is connected with an anti-theft system, and the system automatically alarms when two adjacent houses are robbed the same day.
Given a list of nonnegative integers, the amount of money stored in each house is counted, and if you Rob tonight, you can get as much money as you want without triggering the alarm device .
challenges: O (n) time complexity and O (1) storage.
Problem Solving :
(i) definition dp[i] means the maximum benefit obtained by robbing the first room, while the value of Dp[i] is only related to dp[i-2] and dp[i-3], and dp[i] = A[i] + max (dp[i-2],dp[i-3))
When all A[i] are solved, the last two dp[len-1] dp[len-2] should be taken as the final answer.
Public LongHouserobber (int[] A) {intLen =a.length; if(len==0) return0; //Dp[i] "expression of the looting of the room until the harvest, and Dp[i-2] dp[i-3] about Long[] DP =New Long[Len]; dp[0] = a[0]; if(len==1){ returnDp[0]; }Else if(len==2) {dp[1] = a[1]; returnMath.max (Dp[0], dp[1]); }Else if(len==3) {dp[1] = a[1]; dp[2] = a[2]; returnMath.max (dp[1], dp[2]); } dp[1] = a[1]; dp[2] = a[0] + a[2]; for(inti=3; i<len; i++) {Dp[i]= A[i] + math.max (dp[i-2], dp[i-3]); } returnMath.max (Dp[len-2], dp[len-1]);}
(b) do not use arrays. O (n) time complexity and O (1) storage
I-3 |
I-2 |
I-1 |
I |
Max0 |
Max1 |
Max2 |
Max3 |
Maximum value at max3 = a[i] + max (max1,max0)
When it is i+1, update max0, MAX1, MAX2
max0 = Max1
MAX1 = Max2
MAX2 = Max3
Public LongHouserobber (int[] A) { intLen =a.length; if(len==0)return0; if(len==1)returnA[0]; if(len==2)returnMath.max (A[0], a[1]); if(len==3)returnMath.max (a[1], a[0]+a[2]); Longmax0, Max1, Max2, max3; Max0= A[0]; Max1= A[1]; Max2= Math.max (Max1, a[2]+max0); Max3= 0; for(inti=3; i<len; i++) {max3= A[i] +Math.max (max0, MAX1); Max0=Max1; Max1=Max2; Max2=max3; } returnMath.max (max3, max1);}
House Robbery II Topic
After the last robbery, the thief found a new place to rob, but this time all the houses were in a circle, which meant that the first house and the last house were next to each other. Each house is stocked with a certain amount of money. The only constraint you face is that the neighboring house is connected with an anti-theft system, and the system automatically alarms when two adjacent houses are robbed the same day.
Given a list of nonnegative integers, the amount of money stored in each house is counted, and if you Rob tonight, you can get as much money as you want without triggering the alarm device.
Solving:
According to the title, the No. 0 room and the first n-1 room can only rob one of them.
The range of robbery can only be: 0-n-2 or 1-n-1
So according to the procedure of robbing room I, revise to rob according to the scope, and then seek the maximum value of this two robbery.
Public intHouseRobber2 (int[] nums) { //Write your code here if(nums==NULL|| nums.length==0)return0; intLen =nums.length; if(len==1)returnNums[0]; if(len==2)returnMath.max (Nums[0], nums[1]); if(len==3)returnMath.max (Math.max (nums[1], nums[0]), nums[2]); intRes1 = Houserobber (nums, 0, Len-2); intRes2 = Houserobber (nums, 1, len-1); returnMath.max (res1, res2);}//Rob House I (robbery range)Private intHouserobber (int[] Nums,intStartintend) { if(Start==end)returnNums[start]; if(start+1 = = end)returnMath.max (Nums[start], nums[end]); if(start+2 = =end)returnMath.max (nums[start+1], nums[start]+Nums[end]); intLen =nums.length; int[] DP =New int[Len]; Dp[start]=Nums[start]; Dp[start+1] = nums[start+1]; Dp[start+2] = nums[start+2] +Dp[start]; for(inti=start+3; i<=end; i++) {Dp[i]= Nums[i] + math.max (dp[i-2], dp[i-3]); } returnMath.max (Dp[end], dp[end-1]);}
House robbery I, II