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 45a9bd0 commit dc7692aCopy full SHA for dc7692a
solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.js
@@ -1,8 +1,14 @@
1
function minCostClimbingStairs(cost) {
2
const n = cost.length;
3
- const f = 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]);
6
- }
7
- return f[n];
+ const f = Array(n).fill(-1);
+ const dfs = i => {
+ if (i >= n) {
+ return 0;
+ }
8
+ if (f[i] < 0) {
9
+ f[i] = cost[i] + Math.min(dfs(i + 1), dfs(i + 2));
10
11
+ return f[i];
12
+ };
13
+ return Math.min(dfs(0), dfs(1));
14
}
0 commit comments