Skip to content

Commit ccdefcb

Browse files
authored
Update Solution3.ts
1 parent 4f59de6 commit ccdefcb

File tree

1 file changed

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

1 file changed

+5
-13
lines changed
Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
function minCostClimbingStairs(cost: number[]): number {
2-
const n = cost.length;
3-
const cache = Array(n).fill(-1);
4-
5-
const fn = (i: number): number => {
6-
if (i <= 1) return cost[i];
7-
if (cache[i] !== undefined && cache[i] !== -1) return cache[i];
8-
9-
cache[i] = (cost[i] ?? 0) + Math.min(fn(i - 1), fn(i - 2));
10-
11-
return cache[i];
12-
};
13-
14-
return fn(n);
2+
let [f, g] = [0, 0];
3+
for (let i = 1; i < cost.length; ++i) {
4+
[f, g] = [g, Math.min(f + cost[i - 1], g + cost[i])];
5+
}
6+
return g;
157
}

0 commit comments

Comments
 (0)