Facebook Pixel

Min Cost Climbing Stairs

You are given an integer array cost where cost[i] is the cost of the i-th step on a staircase. Once you pay the cost of a step, you can climb either one or two steps from it.

You may start from the step at index 0 or the step at index 1, and starting is free. Return the minimum cost to reach the top of the floor, which is the position one past the last index.

Input & Output
Input
cost — the cost of stepping off each stair
Output
the minimum total cost to reach the top of the floor
Example
Input
cost = [10, 15, 20]
Output
15
Explanation

Start at index 1, pay 15, and climb two steps to land past the end. Starting at index 0 instead would cost at least 10 + 15 or 10 + 20.

Example
Input
cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output
6
Explanation

Start at index 0 and pay at indices 0, 2, 4, 6, 7, and 9, each costing 1, stepping over both 100s. The total is 6.

Constraints
  • 2 <= cost.length <= 1000
  • 0 <= cost[i] <= 999

Try it yourself

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro