Skip to content

Commit d9c7fd1

Browse files
authored
Update Solution2.ts
1 parent ec52000 commit d9c7fd1

File tree

1 file changed

+5
-5
lines changed
  • solution/0700-0799/0746.Min Cost Climbing Stairs

1 file changed

+5
-5
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
function minCostClimbingStairs(cost: number[]): number {
2-
let a = 0,
3-
b = 0;
4-
for (let i = 1; i < cost.length; ++i) {
5-
[a, b] = [b, Math.min(a + cost[i - 1], b + cost[i])];
2+
const n = cost.length;
3+
const f: number[] = Array(n + 1).fill(0);
4+
for (let i = 2; i <= n; ++i) {
5+
f[i] = Math.min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]);
66
}
7-
return b;
7+
return f[n];
88
}

0 commit comments

Comments
 (0)