-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathres.ts
More file actions
27 lines (22 loc) · 646 Bytes
/
res.ts
File metadata and controls
27 lines (22 loc) · 646 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function internalRob(nums: number[]): number {
if (!nums.length) {
return 0;
}
const dp: number[] = new Array(nums.length).fill(0);
for (let i = 0; i < nums.length; i++) {
if (i === 0) {
dp[i] = nums[i];
} else if (i === 1) {
dp[i] = Math.max(nums[i], dp[i-1]);
} else {
dp[i] = Math.max(nums[i] + dp[i-2], dp[i-1]);
}
}
return dp[nums.length-1];
};
function rob(nums: number[]): number {
if (!nums.length) {
return 0;
}
return Math.max(internalRob(nums.slice(1)), internalRob(nums.slice(2, nums.length-1)) + nums[0]);
};