Skip to content

Commit d0276ca

Browse files
authored
Update Solution2.cpp
1 parent 14e167d commit d0276ca

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
class Solution {
22
public:
33
int minCostClimbingStairs(vector<int>& cost) {
4-
int f = 0, g = 0;
5-
for (int i = 2; i <= cost.size(); ++i) {
6-
int gg = min(f + cost[i - 2], g + cost[i - 1]);
7-
f = g;
8-
g = gg;
4+
int n = cost.size();
5+
vector<int> f(n + 1);
6+
for (int i = 2; i <= n; ++i) {
7+
f[i] = min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]);
98
}
10-
return g;
9+
return f[n];
1110
}
12-
};
11+
};

0 commit comments

Comments
 (0)