diff --git a/problems/0198.打家劫舍.md b/problems/0198.打家劫舍.md index 63a68c36..5b5863a4 100644 --- a/problems/0198.打家劫舍.md +++ b/problems/0198.打家劫舍.md @@ -25,7 +25,7 @@ 输出:12 解释:偷窃 1 号房屋 (金额 = 2), 偷窃 3 号房屋 (金额 = 9),接着偷窃 5 号房屋 (金额 = 1)。   偷窃到的最高金额 = 2 + 9 + 1 = 12 。 -  + 提示: @@ -175,6 +175,22 @@ func max(a, b int) int { } ``` +JavaScript: + +```javascript +const rob = nums => { + // 数组长度 + const len = nums.length; + // dp数组初始化 + const dp = [nums[0], Math.max(nums[0], nums[1])]; + // 从下标2开始遍历 + for (let i = 2; i < len; i++) { + dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]); + } + return dp[len - 1]; +}; +``` +