On a staircase, the i
-th step has some non-negative cost cost[i]
assigned (0 indexed).
Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top floor, given that you can either start from the step with index 0 or the step with index 1.
___________ ___ | Final Step ___ | 20 ___ | 15 __________ | 10 First Step
Example 1:
Input: cost = [10, 15, 20] Output: 15 Explanation: Cheapest is start on cost[1], pay that cost and go to the top.
Example 2:
Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1] Output: 6 Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3].
Note:
cost
will have a length in the range [2, 1000]
.cost[i]
will be an integer in the range [0, 999]
.The core challenge of this problem is to find the minimum cost to reach the top of the staircase. The significance of this problem lies in its application to dynamic programming, where we need to make optimal decisions at each step to minimize the total cost. A common pitfall is to assume that starting from the first step is always the best option, which is not necessarily true.
To solve this problem, we can use dynamic programming. The idea is to keep track of the minimum cost to reach each step and use this information to calculate the minimum cost to reach the top.
A naive solution would involve recursively calculating the cost for each step, but this approach is not optimal due to its exponential time complexity.
We can optimize the solution by using a bottom-up dynamic programming approach. We will maintain an array dp
where dp[i]
represents the minimum cost to reach step i
. The recurrence relation will be:
dp[i] = cost[i] + min(dp[i-1], dp[i-2])
We can further optimize the space complexity by using two variables instead of an array to keep track of the minimum costs for the last two steps.
1. Initialize two variables to store the minimum cost to reach the last two steps.
2. Iterate through the cost array and update the variables based on the recurrence relation.
3. Return the minimum of the two variables as the result.
public class MinCostClimbingStairs {
public int minCostClimbingStairs(int[] cost) {
// Initialize the minimum costs for the first two steps
int cost1 = 0;
int cost2 = 0;
// Iterate through the cost array
for (int i = 0; i < cost.length; i++) {
// Calculate the current cost
int currentCost = cost[i] + Math.min(cost1, cost2);
// Update the costs for the next iteration
cost1 = cost2;
cost2 = currentCost;
}
// Return the minimum cost to reach the top
return Math.min(cost1, cost2);
}
public static void main(String[] args) {
MinCostClimbingStairs solution = new MinCostClimbingStairs();
int[] cost1 = {10, 15, 20};
int[] cost2 = {1, 100, 1, 1, 1, 100, 1, 1, 100, 1};
System.out.println(solution.minCostClimbingStairs(cost1)); // Output: 15
System.out.println(solution.minCostClimbingStairs(cost2)); // Output: 6
}
}
The time complexity of the optimized solution is O(n)
, where n
is the length of the cost array. This is because we iterate through the array once. The space complexity is O(1)
since we only use two additional variables.
Potential edge cases include:
Each algorithm handles these edge cases effectively by following the same logic.
To test the solution comprehensively, consider the following test cases:
Using a testing framework like JUnit can help automate and validate these test cases.
When approaching such problems, it's essential to:
Practicing similar problems and studying dynamic programming techniques can significantly improve problem-solving skills.
In this blog post, we discussed the Min Cost Climbing Stairs problem, explored various approaches to solve it, and provided a detailed Java implementation. Understanding and solving such problems is crucial for developing strong algorithmic thinking and problem-solving skills.
For further reading and practice, consider the following resources: