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 14e167d commit d0276caCopy full SHA for d0276ca
solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.cpp
@@ -1,12 +1,11 @@
1
class Solution {
2
public:
3
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;
+ int n = cost.size();
+ vector<int> f(n + 1);
+ for (int i = 2; i <= n; ++i) {
+ f[i] = min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]);
9
}
10
- return g;
+ return f[n];
11
12
-};
+};
0 commit comments