We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2b8dd26 commit 504e67fCopy full SHA for 504e67f
solution/0100-0199/0198.House Robber/Solution2.ts
@@ -1,7 +1,9 @@
1
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];
+ const n = nums.length;
+ const f: number[] = Array(n + 1).fill(0);
+ 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]);
7
}
- return Math.max(f, g);
8
+ return f[n];
9
0 commit comments