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 4f59de6 commit ccdefcbCopy full SHA for ccdefcb
solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.ts
@@ -1,15 +1,7 @@
1
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);
+ let [f, g] = [0, 0];
+ for (let i = 1; i < cost.length; ++i) {
+ [f, g] = [g, Math.min(f + cost[i - 1], g + cost[i])];
+ }
+ return g;
15
}
0 commit comments