Back

Min Cost Climbing Stairs

easy

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, you can climb either one or two steps.

You can start from the step with index 0, or the step with index 1.

Return the minimum cost to reach the top of the floor.

Test Cases

Copy an input into the main harness and Run to verify
Input
cost = [10,15,20]
Expected Output
15

Explanation: Start at index 1, pay 15, and climb two steps to the top.

Input
cost = [1,100,1,1,1,100,1,1,100,1]
Expected Output
6

Explanation: Start at index 0 and step on all the 1's, paying 6 in total.

Constraints

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

Hints

Hint 1 — click to reveal

The 'top' is one step past the last index — you do not pay for it.

Hint 2 — click to reveal

The cheapest way to reach a step is its own cost plus the cheaper of the two steps below it.

Java Compiler

Powered by OneCompiler. Starter code loads automatically — edit and hit Run.