Start learning free

LESSON 523 OF 673 · DYNAMIC PROGRAMMING

Min Cost Climbing Stairs - Python Solution and Time Complexity Analysis

Practice O(n) Read the full explanation

You get a list cost for a staircase. You pay cost[i] when you stand on step i.

Return the smallest total you can pay to reach the top.

You start on step 0 or step 1, as you like. After you pay for a step, you climb 1 or 2 steps. The top is one step above the last step.

                        ______ top
                  __6__|
            __3__|
      __9__|
__4__|

Example 1:

Input: cost = [4, 9, 3, 6]
Output: 7
Explanation: This is the staircase in the picture.
Start on step 0 and pay 4.
Climb 2 to step 2 and pay 3.
Climb 2 more to reach the top.
In total, 4 + 3 = 7.

Example 2:

Input: cost = [5, 0]
Output: 0
Explanation: Start on step 1 and pay 0.
Climb 1 to reach the top.
The 5 on step 0 is never paid.

Example 3:

Input: cost = [2, 7, 1, 1, 8, 3, 2]
Output: 7
Explanation: Stand on steps 0, 2, 3 and 5.
That costs 2 + 1 + 1 + 3 = 7.
From step 5, climbing 2 reaches the top.

Constraints:

  • cost has 2 to 1,000 steps.
  • Each cost is from 0 to 999.

STEP 1 OF 3
    Ctrl + Enter to check

    PYTHON · DYNAMIC PROGRAMMING

    Min Cost Climbing Stairs in Python, explained

    Understanding the Problem

    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 we must start from the first step, but we can start from either the first or the second step.

    Approach

    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 next steps.

    Naive Solution

    A naive solution would involve recursively calculating the cost to reach the top from each step, but this approach would be highly inefficient due to repeated calculations.

    Optimized Solution

    We can optimize the solution by using a 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.

    Algorithm

    1. Initialize two variables to store the minimum cost to reach the last two steps.

    2. Iterate through the cost array, updating the minimum cost for each step using the recurrence relation.

    3. Return the minimum cost to reach the top, which will be the minimum of the last two steps.

    Code Implementation

    def minCostClimbingStairs(cost):
        # Initialize the first two steps
        first = cost[0]
        second = cost[1]
        
        # Iterate through the cost array starting from the third step
        for i in range(2, len(cost)):
            current = cost[i] + min(first, second)
            first = second
            second = current
        
        # The minimum cost to reach the top is the minimum of the last two steps
        return min(first, second)
    
    # Example usage
    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
    

    Complexity Analysis

    The time complexity of this approach is O(n) where n is the length of the cost array, as we iterate through the array once. The space complexity is O(1) since we only use two variables to store the minimum costs for the last two steps.

    Edge Cases

    Potential edge cases include:

    • Cost array with only two elements.
    • Cost array with all elements being zero.
    • Cost array with maximum possible values.

    Each of these cases should be tested to ensure the algorithm handles them correctly.

    Testing

    To test the solution comprehensively, we should include a variety of test cases:

    • Simple cases with small arrays.
    • Cases with large arrays.
    • Cases with varying costs.

    Using a testing framework like unittest in Python can help automate and organize these tests.

    Thinking and Problem-Solving Tips

    When approaching such problems, it's important to:

    • Understand the problem constraints and requirements.
    • Break down the problem into smaller subproblems.
    • Consider both naive and optimized solutions.
    • Analyze the time and space complexity of your solutions.

    Practicing similar problems and studying dynamic programming techniques can help improve problem-solving skills.

    Conclusion

    In this blog post, we discussed the problem of finding the minimum cost to climb stairs. We explored a dynamic programming approach to solve the problem efficiently and analyzed its complexity. Understanding and solving such problems is crucial for developing strong algorithmic skills.

    Additional Resources

    For further reading and practice, consider the following resources:

    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free