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 fd82790 commit 64322deCopy full SHA for 64322de
solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.js
@@ -1,15 +1,7 @@
1
function minCostClimbingStairs(cost) {
2
- const n = cost.length;
3
- const cache = Array(n).fill(-1);
4
-
5
- const fn = i => {
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