Skip to content

Commit 4172ba5

Browse files
committed
Add solution #746
1 parent 6904286 commit 4172ba5

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@
160160
722|[Remove Comments](./0722-remove-comments.js)|Medium|
161161
733|[Flood Fill](./0733-flood-fill.js)|Easy|
162162
739|[Daily Temperatures](./0739-daily-temperatures.js)|Medium|
163+
746|[Min Cost Climbing Stairs](./0746-min-cost-climbing-stairs.js)|Easy|
163164
762|[Prime Number of Set Bits in Binary Representation](./0762-prime-number-of-set-bits-in-binary-representation.js)|Easy|
164165
784|[Letter Case Permutation](./0784-letter-case-permutation.js)|Medium|
165166
791|[Custom Sort String](./0791-custom-sort-string.js)|Medium|
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 746. Min Cost Climbing Stairs
3+
* https://leetcode.com/problems/min-cost-climbing-stairs/
4+
* Difficulty: Easy
5+
*
6+
* You are given an integer array cost where cost[i] is the cost of ith step on a staircase.
7+
* Once you pay the cost, you can either climb one or two steps.
8+
*
9+
* You can either start from the step with index 0, or the step with index 1.
10+
*
11+
* Return the minimum cost to reach the top of the floor.
12+
*/
13+
14+
/**
15+
* @param {number[]} cost
16+
* @return {number}
17+
*/
18+
var minCostClimbingStairs = function(cost) {
19+
let [first, second] = cost;
20+
21+
for (let index = 2; index < cost.length; index++) {
22+
const cursor = cost[index] + Math.min(first, second);
23+
first = second;
24+
second = cursor;
25+
}
26+
27+
return Math.min(first, second);
28+
};

0 commit comments

Comments
 (0)