File tree Expand file tree Collapse file tree 3 files changed +55
-0
lines changed
solution/0700-0799/0746.Min Cost Climbing Stairs Expand file tree Collapse file tree 3 files changed +55
-0
lines changed Original file line number Diff line number Diff line change @@ -292,6 +292,26 @@ function minCostClimbingStairs(cost: number[]): number {
292
292
}
293
293
```
294
294
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
+
295
315
<!-- tabs:end -->
296
316
297
317
<!-- solution:end -->
Original file line number Diff line number Diff line change @@ -290,6 +290,26 @@ function minCostClimbingStairs(cost: number[]): number {
290
290
}
291
291
```
292
292
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
+
293
313
<!-- tabs:end -->
294
314
295
315
<!-- solution:end -->
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments