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 stairi - 1or stairi - 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
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 top04Watch it work
dp minimum costs
stair costs
Stairs 0 and 1 cost nothing to reach: dp[0] = 0, dp[1] = 0.
Pseudocode
- 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
Variables
- dp[0]
- 0
- dp[1]
- 0
05Step-by-step dry run
For cost = [10, 15, 20]:
- Starting positions
0and1are free:twoBack = 0,oneBack = 0. - 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.
- From stair 1:
- 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.
- From stair 2:
- Return
oneBack = 15(start on stair 1, jump 2 steps directly to top).
06Java solution
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}));
}
}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)wheren = cost.length. - Space complexity:
O(1)using two rolling integers.
09Edge cases
cost.length == 2: Can step immediately from index0or1with 2-step jump; returnsmin(cost[0], cost[1]).- Zero costs: Costs can be
0; algorithm naturally prioritizes free steps.
10Common mistakes
- Stopping at index
n - 1instead ofn: The top of the floor is positionn, which is one step past the last element incost. - Charging cost on arrival rather than departure: The problem states you pay
cost[i]when you take a step from stairi.
11Other approaches
- Modify input array in place:
cost[i] += Math.min(cost[i - 1], cost[i - 2]). TakesO(1)space without extra variables, but mutates the input array.
12In the interview
- Clarify that "top of the floor" means index
n, beyond the end of the array. - 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.