AlgoLounge

Min Cost Climbing Stairs

easy

Leetcode 746. Min Cost Climbing Stairs

You are given an array of integers cost where cost[i] is the cost of taking a step from the ith floor of a staircase. After paying the cost, you can step to either the (i + 1)th floor or the (i + 2)th floor.

You may choose to start at the index 0 or the index 1 floor.

Return the minimum cost to reach the top of the staircase, i.e. just past the last index in cost.

Examples

Example 1

Input:  cost = [1,2,3]
Output: 2

We can start at index = 1 and pay the cost of cost[1] = 2 and take two steps to reach the top. The total cost is 2.

Example 2

Input:  cost = [1,2,1,2,1,1,1]
Output: 4

Start at index = 0. - Pay the cost of cost[0] = 1 and take two steps to reach index = 2. - Pay the cost of cost[2] = 1 and take two steps to reach index = 4. - Pay the cost of cost[4] = 1 and take two steps to reach index = 6. - Pay the cost of cost[6] = 1 and take one step to reach the top. - The total cost is 4.

Constraints

  • 2 <= cost.length <= 100
  • 0 <= cost[i] <= 100
TypeScript
Loading editor…
List of test cases (10)
Case 1
cost = [10,15,20]
Case 2
cost = [1,100,1,1,1,100,1,1,100,1]
Case 3
cost = [1,2,3]
Case 4
cost = [1,2,1,2,1,1,1]
Case 5
cost = [0,0,0,1]
Case 6
cost = [1,0,0,0]
Case 7
cost = [5,10]
Case 8
cost = [10,5]
Case 9
cost = [1,2,3,4,5]
Case 10
cost = [0,1,2,2]