Skip to content

Commit 504e67f

Browse files
authored
Update Solution2.ts
1 parent 2b8dd26 commit 504e67f

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
function rob(nums: number[]): number {
2-
let [f, g] = [0, 0];
3-
for (const x of nums) {
4-
[f, g] = [Math.max(f, g), f + x];
2+
const n = nums.length;
3+
const f: number[] = Array(n + 1).fill(0);
4+
f[1] = nums[0];
5+
for (let i = 2; i <= n; ++i) {
6+
f[i] = Math.max(f[i - 1], f[i - 2] + nums[i - 1]);
57
}
6-
return Math.max(f, g);
8+
return f[n];
79
}

0 commit comments

Comments
 (0)