Skip to content

Commit 64f633c

Browse files
committed
house-robber solution
1 parent 372b12e commit 64f633c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

house-robber/yuhyeon99.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+
const memo = {};
7+
8+
function dfs(start) {
9+
if(memo[start] !== undefined) return memo[start];
10+
if(start >= nums.length) {
11+
memo[start] = 0;
12+
} else {
13+
memo[start] = Math.max(nums[start] + dfs(start + 2), dfs(start + 1))
14+
}
15+
return memo[start];
16+
}
17+
18+
return dfs(0);
19+
};

0 commit comments

Comments
 (0)