2024/12/9 灵茶题单 dp
This commit is contained in:
parent
4319230aa4
commit
1fbd98950b
|
@ -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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue