diff --git a/src/main/java/com/cool/ling_cha_mount/dp/Num213.java b/src/main/java/com/cool/ling_cha_mount/dp/Num213.java new file mode 100644 index 0000000..2f9c938 --- /dev/null +++ b/src/main/java/com/cool/ling_cha_mount/dp/Num213.java @@ -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]; + } +} +