Min Cost Climbing Stairs Leetcode Problem 746 [Python Solution]
Welcome to another coding adventure!
Today, we're going to tackle the Min Cost Climbing Stairs problem from LeetCode.
This problem falls under the category of 1-D Dynamic Programming and is considered easy.
We'll walk you through the problem, provide both a brute-force approach and an efficient dynamic programming solution in Python, and explain the reasoning behind our approach.
Problem Overview
You are given an integer array called cost
, where cost[i]
represents the cost of stepping on the i-th
step of a staircase.
The interesting twist here is that, after paying the cost, you can choose to climb either one step or two steps.
Additionally, you have the option to start from either the first step (cost[0]
) or the second step (cost[1]
).
The objective is to determine the minimum cost required to reach the top of the staircase, which is not explicitly mentioned in the problem statement.
However, for this problem, the top of the staircase can be considered as the step that follows the last element in the cost
array.
Let's illustrate this with an example:
Example 1:
Input:
cost = [10, 15, 20]
Output:
15
Explanation:
You will start at index 1 (with a cost of 15) and have two options:
1. Pay 15 and climb two steps to reach the top, incurring a total cost of 15.
2. Pay 10, climb two steps to reach index 2, and then proceed to the top.
This route would result in a total cost of 10 + 20 = 30. Clearly, the minimum cost is 15, which you achieve by starting at index 1 and making the double jump.
Example 2:
Input:
cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output:
6
Explanation:
Starting at index 0 (with a cost of 1), you can follow these steps:
– Pay 1 and climb two steps to reach index 2.
– Pay 1 and climb two steps to reach index 4.
– Pay 1 and climb two steps to reach index 6.
– Pay 1 and climb one step to reach index 7.
– Pay 1 and climb two steps to reach index 9.
– Pay 1 and climb one step to reach the top.
The total cost for this path is 6, which is the minimum cost.
Constraints:
2 <= cost.length <= 1000
0 <= cost[i] <= 999
In the examples above, we've seen that simply choosing the cheapest step at each stage won't always lead to the minimum cost due to the possibility of encountering more expensive steps later.
To solve this problem efficiently, we will use dynamic programming.
Understanding the Constraints
Before we dive into the solution, it's essential to understand the problem's constraints:
- The number of steps in the staircase can vary from 2 to 1000.
- The cost associated with each step can range from 0 to 999.
Brute-force Approach
The brute-force approach is a simple way to tackle this problem.
It involves exploring all possible paths through the staircase and calculating the cost for each path.
Among these paths, we'll identify the one with the minimum cost.
However, this approach can be quite slow for larger inputs due to its exponential time complexity.
To visualize this approach, let's create a decision tree.
We'll start at the initial step (either index 0 or 1) and explore both single and double jump options until we reach the top.
We'll keep track of the total cost for each path.
Consider the following decision tree for the cost = [10, 15, 20]
example, starting from index 0:
0
/
1 2
/
2 3
/
3
In this tree:
– We start at index 0 with a cost of 10.
– At index 1, we have two choices: a single jump to index 2 (cost 15) or a double jump to index 3 (cost 20).
– At index 2, we can only make a single jump to index 3, incurring a cost of 20.
– At index 3, we've reached the top (out of bounds).
To find the minimum cost, we calculate the total cost for each path:
– Path 1: 10 (index 0) + 15 (index 1) + 20 (index 2) = 45
– Path 2: 10 (index 0) + 20 (index 2) = 30
– Path 3: 10 (index 0) + 15 (index 1) + 20 (index 3) = 45
As we can see, the minimum cost is 30, which is the answer.
This approach works by exploring all possible paths, but it's highly inefficient for large input sizes because it has an exponential time complexity.
For each step, you need to consider both single and double jump options, leading to repeated calculations.
Efficient Approach with Dynamic Programming
To improve efficiency, we'll use dynamic programming.
Dynamic programming is an optimization technique that involves breaking down a problem into smaller subproblems and solving each subproblem only once.
We'll use a bottom-up approach to calculate the minimum cost iteratively, avoiding redundant calculations.
Here's how the efficient dynamic programming solution works:
- We'll start by appending a
0
to the end of thecost
array.
This extra step helps simplify the logic when we reach the top, as there's no cost associated with the top step.
- We'll initialize an array
dp
to store the minimum cost to reach each step.
The length of this array will be equal to the length of the cost
array.
-
We'll iterate through the
cost
array in reverse order, from the second-to-last step to the first step. -
At each step, we have two choices:
- Take a single jump and add the cost of the current step to the minimum cost of reaching the next step.
- Take a double jump and add the cost of the current step to the minimum cost of reaching the step after the next step.
-
We'll update the
dp
array with the minimum of these two choices. -
Finally, the minimum cost to reach the top will be the minimum of the first two values in the
dp
array, which corresponds to starting at index 0 or 1. This dynamic programming approach ensures that we only calculate the minimum cost for each step once and avoids redundant computations.
It has a time complexity of O(n)
, where n is the length of the cost
array, and a space complexity of O(n)
to store the dp
array.
Now, let's dive into the Python code for this efficient approach:
Efficient Python Code Solution
def minCostClimbingStairs(cost):
# Append a 0 to the end of the cost array for simplicity.
cost.append(0)
# Initialize the dp array to store minimum cost.
dp = [0] * len(cost)
# Start from the second-to-last step and work backwards.
for i in range(len(cost) - 3, -1, -1):
dp[i] = cost[i] + min(dp[i + 1], dp[i + 2])
# Minimum cost is the minimum of starting at index 0 or 1. return min(dp[0], dp[1])
cost1 = [10, 15, 20]
print(minCostClimbingStairs(cost1)) # Output: 15
cost2 = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
print(minCostClimbingStairs(cost2)) # Output: 6
In this code:
– We start by appending a 0
to the end of the cost
array.
– We initialize the dp
array with zeros.
– We iterate through the cost
array in reverse order, filling in the dp
array.
– Finally, we return the minimum of the first two values in the dp
array as the minimum cost.
This efficient dynamic programming solution gives you the correct answer with optimal time complexity.
Related Interview Questions By Company:
- Find All Numbers Disappeared In An Array LeetCode
- Palindrome Linked List LeetCode
- Linked List Cycle LeetCode
Related Interview Questions By Difficulty:
Related Interview Questions By Category:
Conclusion
We've explored the Min Cost Climbing Stairs problem on LeetCode, which involves finding the minimum cost to reach the top of a staircase while having the option to take one or two steps at each stage.
We've covered both a brute-force approach and an efficient dynamic programming solution in Python.
The dynamic programming solution optimizes the problem by avoiding redundant calculations and has a time complexity of O(n)
, making it suitable for large inputs.
By understanding the problem's constraints and leveraging dynamic programming techniques, you can efficiently solve a wide range of algorithmic problems.