747. Min Cost Climbing Stairs
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the minimum cost to climb a staircase, where cost[i] is the fee to use step i. After paying the cost for a step, we can climb either one or two steps. We are allowed to start from either step 0 or step 1. The goal is to reach the "top of the floor," which can be thought of as the position just after the last step.
Solution
Min Cost Climbing Stairs in Python
class Solution:
def minCostClimbingStairs(self, cost: list[int]) -> int:
n = len(cost)
# 'two_steps_back' stores the minimum cost to reach step i-2.
# Corresponds to dp[i-2] in a DP array solution.
two_steps_back = 0 # Base case: min cost to reach step 0 is 0.
# 'one_step_back' stores the minimum cost to reach step i-1.
# Corresponds to dp[i-1] in a DP array solution.
one_step_back = 0 # Base case: min cost to reach step 1 is 0.
# Iterate from step 2 to the top (which is conceptually step n).
for i in range(2, n + 1):
# The recurrence relation is dp[i] = min(dp[i-1] + cost[i-1], dp[i-2] + cost[i-2]).
# Here, dp[i-1] is one_step_back and dp[i-2] is two_steps_back.
current_cost = min(one_step_back + cost[i - 1], two_steps_back + cost[i - 2])
# Update the variables for the next iteration using tuple unpacking.
# The old 'one_step_back' becomes the new 'two_steps_back'.
# The 'current_cost' (which is dp[i]) becomes the new 'one_step_back'.
two_steps_back, one_step_back = one_step_back, current_cost
# The final answer is the min cost to reach the top (step n), which is stored in one_step_back.
return one_step_back
Min Cost Climbing Stairs in Java
class Solution {
public int minCostClimbingStairs(int[] cost) {
int n = cost.length;
// 'twoStepsBack' stores the minimum cost to reach step i-2.
// Corresponds to dp[i-2] in a DP array solution.
int twoStepsBack = 0; // Base case: min cost to reach step 0 is 0.
// 'oneStepBack' stores the minimum cost to reach step i-1.
// Corresponds to dp[i-1] in a DP array solution.
int oneStepBack = 0; // Base case: min cost to reach step 1 is 0.
// Iterate from step 2 to the top (which is conceptually step n).
for (int i = 2; i <= n; i++) {
// The recurrence relation is dp[i] = min(dp[i-1] + cost[i-1], dp[i-2] + cost[i-2]).
// Here, dp[i-1] is oneStepBack and dp[i-2] is twoStepsBack.
int currentCost = Math.min(oneStepBack + cost[i - 1], twoStepsBack + cost[i - 2]);
// Update the variables for the next iteration.
// The old 'oneStepBack' becomes the new 'twoStepsBack'.
twoStepsBack = oneStepBack;
// The 'currentCost' (which is dp[i]) becomes the new 'oneStepBack'.
oneStepBack = currentCost;
}
// The final answer is the min cost to reach the top (step n), which is stored in oneStepBack.
return oneStepBack;
}
}
Min Cost Climbing Stairs in C++
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int n = cost.size();
// 'two_steps_back' stores the minimum cost to reach step i-2.
// Corresponds to dp[i-2] in a DP array solution.
int two_steps_back = 0; // Base case: min cost to reach step 0 is 0.
// 'one_step_back' stores the minimum cost to reach step i-1.
// Corresponds to dp[i-1] in a DP array solution.
int one_step_back = 0; // Base case: min cost to reach step 1 is 0.
// Iterate from step 2 to the top (which is conceptually step n).
for (int i = 2; i <= n; ++i) {
// The recurrence relation is dp[i] = min(dp[i-1] + cost[i-1], dp[i-2] + cost[i-2]).
// Here, dp[i-1] is one_step_back and dp[i-2] is two_steps_back.
int current_cost = min(one_step_back + cost[i - 1], two_steps_back + cost[i - 2]);
// Update the variables for the next iteration.
// The old 'one_step_back' becomes the new 'two_steps_back'.
two_steps_back = one_step_back;
// The 'current_cost' (which is dp[i]) becomes the new 'one_step_back'.
one_step_back = current_cost;
}
// The final answer is the min cost to reach the top (step n), which is stored in one_step_back.
return one_step_back;
}
};
Min Cost Climbing Stairs in TypeScript
function minCostClimbingStairs(cost: number[]): number {
const n = cost.length;
// 'twoStepsBack' stores the minimum cost to reach step i-2.
// Corresponds to dp[i-2] in a DP array solution.
let twoStepsBack = 0; // Base case: min cost to reach step 0 is 0.
// 'oneStepBack' stores the minimum cost to reach step i-1.
// Corresponds to dp[i-1] in a DP array solution.
let oneStepBack = 0; // Base case: min cost to reach step 1 is 0.
// Iterate from step 2 to the top (which is conceptually step n).
for (let i = 2; i <= n; i++) {
// The recurrence relation is dp[i] = min(dp[i-1] + cost[i-1], dp[i-2] + cost[i-2]).
// Here, dp[i-1] is oneStepBack and dp[i-2] is twoStepsBack.
const currentCost = Math.min(oneStepBack + cost[i - 1], twoStepsBack + cost[i - 2]);
// Update the variables for the next iteration.
// The old 'oneStepBack' becomes the new 'twoStepsBack'.
twoStepsBack = oneStepBack;
// The 'currentCost' (which is dp[i]) becomes the new 'oneStepBack'.
oneStepBack = currentCost;
}
// The final answer is the min cost to reach the top (step n), which is stored in oneStepBack.
return oneStepBack;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.