Skip to content

Commit 5f89ea1

Browse files
committed
house robber time over DFS
1 parent dfe88cf commit 5f89ea1

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

house-robber/JangAyeon.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var rob = function (nums) {
6+
const N = nums.length;
7+
let answer = 0;
8+
function dfs(lastVisited, curr, t) {
9+
if (curr == N) {
10+
console.log(t);
11+
answer = Math.max(answer, t);
12+
return;
13+
}
14+
for (let idx = curr; idx < N; idx++) {
15+
if (idx != lastVisited + 1) {
16+
dfs(idx, idx + 1, t + nums[idx]);
17+
}
18+
dfs(lastVisited, idx + 1, t);
19+
}
20+
}
21+
dfs(-2, 0, 0);
22+
return answer;
23+
};

0 commit comments

Comments
 (0)