Skip to content

Commit c720466

Browse files
committed
house-robber solved
1 parent d618fcc commit c720466

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

house-robber/hsskey.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var rob = function(nums) {
6+
if (nums.length === 0) return 0;
7+
if (nums.length === 1) return nums[0];
8+
9+
const dp = new Array(nums.length);
10+
11+
dp[0] = nums[0];
12+
dp[1] = Math.max(nums[0], nums[1]);
13+
14+
for (let i = 2; i < nums.length; i++) {
15+
dp[i] = Math.max(nums[i] + dp[i - 2], dp[i - 1]);
16+
}
17+
18+
return dp[nums.length - 1];
19+
};

0 commit comments

Comments
 (0)