Skip to content

Commit 5304e39

Browse files
committed
house robber solution DP
1 parent 5f89ea1 commit 5304e39

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

house-robber/JangAyeon.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,11 @@
55
var rob = function (nums) {
66
const N = nums.length;
77
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-
}
8+
let [rob1, rob2] = [0, 0];
9+
for (let num of nums) {
10+
let temp = Math.max(num + rob1, rob2);
11+
rob1 = rob2;
12+
rob2 = temp;
2013
}
21-
dfs(-2, 0, 0);
22-
return answer;
14+
return rob2;
2315
};

0 commit comments

Comments
 (0)