Skip to content
Veltoryx Technologies
FreeProblem 133● Easyleetcode ↗3 min readUpdated 30 October 2026

Min Cost Climbing Stairs

Find the cheapest way to reach the top stair by stepping 1 or 2 stairs at each stride.

01The problem

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

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

Return the minimum cost to reach the top of the floor (index cost.length, past the last stair).

02How to spot it

  • From stair i, you can jump 1 or 2 stairs.
  • Equivalently, to reach stair i, you must arrive from either stair i - 1 or stair i - 2.
  • You pay the cost of the stair you depart from.
  • Recurrence: dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]).

03Pseudocode

text
dp[0] = dp[1] = 0 # starting is free
for i from 2 to n:
  dp[i] = min(dp[i-1] + cost[i-1],
              dp[i-2] + cost[i-2])
return dp[n] # the top

04Watch it work

Visualizer · step through the algorithmcost = [10,15,20]

dp minimum costs

00
01
02
03

stair costs

100
151
202

Stairs 0 and 1 cost nothing to reach: dp[0] = 0, dp[1] = 0.

Pseudocode

  1. dp[0] = dp[1] = 0 # starting is free
  2. for i from 2 to n:
  3. dp[i] = min(dp[i-1] + cost[i-1],
  4. dp[i-2] + cost[i-2])
  5. return dp[n] # the top

Variables

dp[0]
0
dp[1]
0
step 1 / 4

05Step-by-step dry run

For cost = [10, 15, 20]:

  1. Starting positions 0 and 1 are free: twoBack = 0, oneBack = 0.
  2. Stair 2:
    • From stair 1: oneBack + cost[1] = 0 + 15 = 15.
    • From stair 0: twoBack + cost[0] = 0 + 10 = 10.
    • here = min(15, 10) = 10.
    • Slide: twoBack = 0, oneBack = 10.
  3. Stair 3 (top of floor):
    • From stair 2: oneBack + cost[2] = 10 + 20 = 30.
    • From stair 1: twoBack + cost[1] = 0 + 15 = 15.
    • here = min(30, 15) = 15.
    • Slide: twoBack = 10, oneBack = 15.
  4. Return oneBack = 15 (start on stair 1, jump 2 steps directly to top).

06Java solution

java
public class Main {
    public static int minCostClimbingStairs(int[] cost) {
        int twoBack = 0, oneBack = 0;
        for (int i = 2; i <= cost.length; i++) {
            int here = Math.min(oneBack + cost[i - 1], twoBack + cost[i - 2]);
            twoBack = oneBack;
            oneBack = here;
        }
        return oneBack;
    }

    public static void main(String[] args) {
        System.out.println(minCostClimbingStairs(new int[]{10, 15, 20}));
        System.out.println(minCostClimbingStairs(new int[]{1, 100, 1, 1, 1, 100, 1, 1, 100, 1}));
    }
}
Expected output
15
6

07Why it works

To land on stair i, the preceding step was either i - 1 or i - 2. The optimal cost to reach i is the minimum of paying cost[i - 1] to leap from i - 1 versus paying cost[i - 2] to leap from i - 2. Storing only the previous two minimums allows computing the optimal cost up to index n in linear time and constant space.

08Time and space

  • Time complexity: O(n) where n = cost.length.
  • Space complexity: O(1) using two rolling integers.

09Edge cases

  • cost.length == 2: Can step immediately from index 0 or 1 with 2-step jump; returns min(cost[0], cost[1]).
  • Zero costs: Costs can be 0; algorithm naturally prioritizes free steps.

10Common mistakes

  • Stopping at index n - 1 instead of n: The top of the floor is position n, which is one step past the last element in cost.
  • Charging cost on arrival rather than departure: The problem states you pay cost[i] when you take a step from stair i.

11Other approaches

  • Modify input array in place: cost[i] += Math.min(cost[i - 1], cost[i - 2]). Takes O(1) space without extra variables, but mutates the input array.

12In the interview

  1. Clarify that "top of the floor" means index n, beyond the end of the array.
  2. Note that starting on stair 0 or 1 incurs no cost until you leap from it.

13Quiz

Where is the destination located relative to the array length n?

  • (Recommended) At index n (past the last element)
  • At index n - 1 (the last stair)
  • At index n - 2
  • At index 0

Answer: At index n (past the last element). The goal is to reach the floor above the top step.

14Solve it yourself

Open LeetCode #746 and submit the solution.