You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.2 KiB

package class33;
public class Problem_0213_HouseRobberII {
// arr 长度大于等于1
public static int pickMaxSum(int[] arr) {
int n = arr.length;
// dp[i] : arr[0..i]范围上,随意选择,但是,任何两数不能相邻。得到的最大累加和是多少?
int[] dp = new int[n];
dp[0] = arr[0];
dp[1] = Math.max(arr[0], arr[1]);
for (int i = 2; i < n; i++) {
int p1 = arr[i];
int p2 = dp[i - 1];
int p3 = arr[i] + dp[i - 2];
dp[i] = Math.max(p1, Math.max(p2, p3));
}
return dp[n - 1];
}
public static int rob(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
if (nums.length == 1) {
return nums[0];
}
if (nums.length == 2) {
return Math.max(nums[0], nums[1]);
}
int pre2 = nums[0];
int pre1 = Math.max(nums[0], nums[1]);
for (int i = 2; i < nums.length - 1; i++) {
int tmp = Math.max(pre1, nums[i] + pre2);
pre2 = pre1;
pre1 = tmp;
}
int ans1 = pre1;
pre2 = nums[1];
pre1 = Math.max(nums[1], nums[2]);
for (int i = 3; i < nums.length; i++) {
int tmp = Math.max(pre1, nums[i] + pre2);
pre2 = pre1;
pre1 = tmp;
}
int ans2 = pre1;
return Math.max(ans1, ans2);
}
}