2024/12/9 灵茶题单 dp

This commit is contained in:
Cool 2024-12-09 19:08:57 +08:00
parent 4319230aa4
commit 1fbd98950b
1 changed files with 15 additions and 0 deletions

View File

@ -0,0 +1,15 @@
package com.cool.ling_cha_mount.dp;
public class Num213 {
public int rob(int[] nums) {
return Math.max(nums[0]+rob1(nums,2,nums.length-1),rob1(nums,1,nums.length));
}
public int rob1(int[] nums,int start,int end) {
int[] dp = new int[nums.length + 2];
for (int i = start; i < end; i++) {
dp[i + 2] = Math.max(dp[i] + nums[i], dp[i + 1]);
}
return dp[end+ 1];
}
}