Skip to content

Commit 2bd7c3c

Browse files
authored
Update Solution2.java
1 parent 84d7266 commit 2bd7c3c

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
22
public int rob(int[] nums) {
3-
int f = 0, g = 0;
4-
for (int x : nums) {
5-
int ff = Math.max(f, g);
6-
g = f + x;
7-
f = ff;
3+
int n = nums.length;
4+
int[] f = new int[n + 1];
5+
f[1] = nums[0];
6+
for (int i = 2; i <= n; ++i) {
7+
f[i] = Math.max(f[i - 1], f[i - 2] + nums[i - 1]);
88
}
9-
return Math.max(f, g);
9+
return f[n];
1010
}
11-
}
11+
}

0 commit comments

Comments
 (0)