Skip to content

Commit 47b2de2

Browse files
committed
house robber
1 parent 5406daf commit 47b2de2

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

house-robber/Totschka.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// https://leetcode.com/problems/house-robber/
2+
let dp;
3+
function rob(nums: number[]): number {
4+
dp = Array.from({ length: nums.length + 1 }, () => -1);
5+
return doRobbery(nums, nums.length - 1);
6+
}
7+
8+
function doRobbery(nums: number[], i: number) {
9+
if (i < 0) {
10+
return 0;
11+
}
12+
if (dp[i] >= 0) {
13+
return dp[i];
14+
}
15+
const money = Math.max(doRobbery(nums, i - 2) + nums[i], doRobbery(nums, i - 1));
16+
dp[i] = money;
17+
return money;
18+
}

0 commit comments

Comments
 (0)