Skip to content

Commit 4b57ba1

Browse files
authored
Update Solution2.java
1 parent 8d06a91 commit 4b57ba1

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,11 +1,10 @@
11
class Solution {
22
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;
3+
int n = cost.length;
4+
int[] f = new int[n + 1];
5+
for (int i = 2; i <= n; ++i) {
6+
f[i] = Math.min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]);
87
}
9-
return g;
8+
return f[n];
109
}
11-
}
10+
}

0 commit comments

Comments
 (0)