Skip to content

Commit cbb7de6

Browse files
committed
house robber solution
1 parent 091c502 commit cbb7de6

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

house-robber/hyer0705.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function rob(nums: number[]): number {
2+
const n = nums.length;
3+
4+
const dp: number[][] = Array.from({ length: n }, () => Array(2).fill(0));
5+
6+
dp[0][1] = nums[0];
7+
8+
for (let i = 1; i < n; i++) {
9+
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]);
10+
dp[i][1] = dp[i - 1][0] + nums[i];
11+
}
12+
13+
return Math.max(...dp[n - 1]);
14+
}

0 commit comments

Comments
 (0)