Skip to content

Commit 7a53e2a

Browse files
committed
feat: add js solution to lc problem: No.0746
1 parent 0672aae commit 7a53e2a

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

solution/0700-0799/0746.Min Cost Climbing Stairs/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,26 @@ function minCostClimbingStairs(cost: number[]): number {
292292
}
293293
```
294294

295+
#### JavaScript
296+
297+
```js
298+
function minCostClimbingStairs(cost) {
299+
const n = cost.length;
300+
const cache = Array(n).fill(-1);
301+
302+
const fn = i => {
303+
if (i <= 1) return cost[i];
304+
if (cache[i] !== undefined && cache[i] !== -1) return cache[i];
305+
306+
cache[i] = (cost[i] ?? 0) + Math.min(fn(i - 1), fn(i - 2));
307+
308+
return cache[i];
309+
};
310+
311+
return fn(n);
312+
}
313+
```
314+
295315
<!-- tabs:end -->
296316
297317
<!-- solution:end -->

solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,26 @@ function minCostClimbingStairs(cost: number[]): number {
290290
}
291291
```
292292

293+
#### JavaScript
294+
295+
```js
296+
function minCostClimbingStairs(cost) {
297+
const n = cost.length;
298+
const cache = Array(n).fill(-1);
299+
300+
const fn = i => {
301+
if (i <= 1) return cost[i];
302+
if (cache[i] !== undefined && cache[i] !== -1) return cache[i];
303+
304+
cache[i] = (cost[i] ?? 0) + Math.min(fn(i - 1), fn(i - 2));
305+
306+
return cache[i];
307+
};
308+
309+
return fn(n);
310+
}
311+
```
312+
293313
<!-- tabs:end -->
294314
295315
<!-- solution:end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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);
15+
}

0 commit comments

Comments
 (0)