Skip to content

Commit c35ccf7

Browse files
authored
Update Solution2.cpp
1 parent 844f9fc commit c35ccf7

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
class Solution {
22
public:
33
int rob(vector<int>& nums) {
4-
int f = 0, g = 0;
5-
for (int& x : nums) {
6-
int ff = max(f, g);
7-
g = f + x;
8-
f = ff;
4+
int n = nums.size();
5+
int f[n + 1];
6+
memset(f, 0, sizeof(f));
7+
f[1] = nums[0];
8+
for (int i = 2; i <= n; ++i) {
9+
f[i] = max(f[i - 1], f[i - 2] + nums[i - 1]);
910
}
10-
return max(f, g);
11+
return f[n];
1112
}
12-
};
13+
};

0 commit comments

Comments
 (0)