From 1fbd98950b5168851043100836d38729e35a5cca Mon Sep 17 00:00:00 2001 From: Cool <747682928@qq.com> Date: Mon, 9 Dec 2024 19:08:57 +0800 Subject: [PATCH] =?UTF-8?q?2024/12/9=20=E7=81=B5=E8=8C=B6=E9=A2=98?= =?UTF-8?q?=E5=8D=95=20dp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/cool/ling_cha_mount/dp/Num213.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/main/java/com/cool/ling_cha_mount/dp/Num213.java 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]; + } +} +