Min Cost Climbing Stairs - C++ Solution and Time Complexity Analysis


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:

  1. cost will have a length in the range [2, 1000].
  2. Every cost[i] will be an integer in the range [0, 999].

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 starting from the first step is always the best option, which is not necessarily true.

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 top.

Naive Solution

A naive solution would involve recursively calculating the cost for each step, but this approach is not optimal due to its exponential time complexity.

Optimized Solution

We can optimize the solution using dynamic programming. 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 cost1 and cost2 to 0.

2. Iterate through the cost array and update the variables using the recurrence relation.

3. The final answer will be the minimum of cost1 and cost2.

Code Implementation

#include <vector>
#include <algorithm>
using namespace std;

// Function to calculate the minimum cost to reach the top
int minCostClimbingStairs(vector<int>& cost) {
    int n = cost.size();
    int cost1 = 0, cost2 = 0;

    // Iterate through the cost array
    for (int i = 0; i < n; ++i) {
        int currentCost = cost[i] + min(cost1, cost2);
        cost1 = cost2;
        cost2 = currentCost;
    }

    // The minimum cost to reach the top
    return min(cost1, cost2);
}

// Example usage
int main() {
    vector<int> cost1 = {10, 15, 20};
    vector<int> cost2 = {1, 100, 1, 1, 1, 100, 1, 1, 100, 1};

    // Output: 15
    cout << minCostClimbingStairs(cost1) << endl;

    // Output: 6
    cout << minCostClimbingStairs(cost2) << endl;

    return 0;
}

Complexity Analysis

The time complexity of the optimized solution is O(n), where n is the length of the cost array. The space complexity is O(1) since we are using only two variables to store the minimum costs.

Edge Cases

Potential edge cases include:

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

Testing

To test the solution comprehensively, consider the following test cases:

Using a testing framework like Google Test can help automate and manage these tests effectively.

Thinking and Problem-Solving Tips

When approaching dynamic programming problems, it's crucial to:

Practicing similar problems and studying different algorithms can significantly improve problem-solving skills.

Conclusion

In this blog post, we discussed the "Min Cost Climbing Stairs" problem, explored different approaches to solve it, and provided a detailed C++ implementation. Understanding and solving such problems is essential for developing strong algorithmic thinking and problem-solving skills.

Additional Resources

For further reading and practice, consider the following resources: