From 2481f796285390012b31add2eba88c12e2b5ce26 Mon Sep 17 00:00:00 2001 From: BangDori Date: Tue, 10 Jun 2025 08:35:32 +0900 Subject: [PATCH] =?UTF-8?q?[=EA=B0=95=EB=B3=91=EC=A4=80]=20House=20Robber?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bangdori/198.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 bangdori/198.js diff --git a/bangdori/198.js b/bangdori/198.js new file mode 100644 index 0000000..1edba37 --- /dev/null +++ b/bangdori/198.js @@ -0,0 +1,22 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var rob = function (nums) { + const n = nums.length; + + if (n <= 2) return Math.max(...nums); + + const dp = Array(n).fill(0); + + dp[0] = nums[0]; + dp[1] = nums[1]; + dp[2] = nums[2] + dp[0]; + + for (let i = 3; i < n; i++) { + dp[i] = Math.max(dp[i - 2], dp[i - 3]); + dp[i] += nums[i]; + } + + return Math.max(dp[n - 1], dp[n - 2]); +};