Skip to content

Commit 9252f9c

Browse files
committed
horse robber solution
1 parent 7900ad0 commit 9252f9c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

house-robber/froggy1014.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 + 1);
10+
dp[0] = 0;
11+
dp[1] = nums[0];
12+
for (let i = 2; i < dp.length; i++) {
13+
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]);
14+
}
15+
return dp[dp.length - 1];
16+
};
17+
18+
const nums = [2, 7, 9, 3, 1];
19+
20+
console.log(rob(nums));

0 commit comments

Comments
 (0)