/** 213. House Robber II * 12.30 by Mingyang * Because can't rob two, so I blindly to find the first to be robbed or not, but the fact is divided two times call house robber I. * Case 1: The last element is not included. Case 2: The first element is not included. The maximum value of both is the global maximum * just beginning to enter a misunderstanding, the first do not, the last must be, is not right, because the first do not, the last one does not have to be * the same, attention is the first second need special judgment, respectively, they compare Who's big who's small, followed by DP in turn*/ Public intROB2 (int[] nums) { if(nums==NULL|| nums.length==0)return0; if(nums.length==1)returnNums[0]; if(nums.length==2)returnMath.max (Nums[0], nums[1]); returnMath.max (Rob (nums, 0, Nums.length-2), Rob (Nums, 1, nums.length-1)); } Private intRobint[] Nums,intSinte) {intn = e-s + 1; int[] DP =New int[n]; dp[0] =Nums[s]; dp[1] = Math.max (Nums[s], nums[s+1]); for(intK=p; i<n; i++) {Dp[i]= Math.max (Dp[i-2]+nums[s+i], dp[i-1]); } returnDp[n-1]; }
213 House Robber II