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 8d06a91 commit 4b57ba1Copy full SHA for 4b57ba1
solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.java
@@ -1,11 +1,10 @@
1
class Solution {
2
public int minCostClimbingStairs(int[] cost) {
3
- int f = 0, g = 0;
4
- for (int i = 2; i <= cost.length; ++i) {
5
- int gg = Math.min(f + cost[i - 2], g + cost[i - 1]);
6
- f = g;
7
- g = gg;
+ int n = cost.length;
+ int[] f = new int[n + 1];
+ for (int i = 2; i <= n; ++i) {
+ f[i] = Math.min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]);
8
}
9
- return g;
+ return f[n];
10
11
-}
+}
0 commit comments