Skip to content

Commit a0e36c2

Browse files
authored
Update 198-house-robber.js
1 parent 9039695 commit a0e36c2

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

198-house-robber.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,21 @@ function rob(nums) {
1313
}
1414
return prev1;
1515
}
16+
17+
// another
18+
19+
/**
20+
* @param {number[]} nums
21+
* @return {number}
22+
*/
23+
const rob = function(nums) {
24+
const n = nums.length
25+
const dp = Array(n+1).fill(0)
26+
dp[1] = nums[0]
27+
28+
for(let i = 1; i < n; i++) {
29+
dp[i + 1] = Math.max(dp[i], dp[i - 1] + nums[i])
30+
}
31+
32+
return dp[n]
33+
};

0 commit comments

Comments
 (0)