πŸš€Day 9. Min Cost Climbing Stairs 🧠

The problem can be found at the following link: Question Link

πŸ’‘ Problem Description:

Given an array of integers cost[], where cost[i] represents the cost of the i-th step on a staircase, you can either:

  • Pay the cost at i-th step and move one step forward.

  • Pay the cost at i-th step and move two steps forward.

Return the minimum cost required to reach the top of the floor.

πŸ“Œ Assumptions:

  • 0-based indexing

  • You can start either from step 0 or step 1

πŸ” Example Walkthrough:

Example 1:

Input:

cost[] = [10, 15, 20]

Output:

Explanation:

The cheapest way is:

  • Start at cost[1] = 15

  • Jump 2 steps to the top (no cost at the top)

Example 2:

Input:

Output:

Explanation:

The cheapest way is: 1 β†’ 3 β†’ 4 β†’ 6 β†’ 7 β†’ 9 (Total cost = 1 + 1 + 1 + 1 + 1 + 1 = 6)

Constraints:

  • $(2 \leq \text{cost.length} \leq 1000)$

  • $2 ≀ cost.size() ≀ 10^5$

  • $(0 \leq \text{cost[i]} \leq 999)$

🎯 My Approach:

Algorithm Steps:

  1. Use two variables a and b to keep track of the minimum cost of the last two steps.

  2. Iterate through the cost array, updating b using the recurrence relation:

    • b = cost[i] + min(a, b)

    • a = previous b (before update)

  3. Return min(a, b), representing the minimum cost to reach the top.

πŸ•’ Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(N), as we iterate through the cost array once.

  • Expected Auxiliary Space Complexity: O(1), as we use only a constant amount of extra space (a and b).

πŸ“ Solution Code

Code (C++)

⚑ Alternative Approaches

1️⃣ Dynamic Programming (O(N) Time, O(N) Space) β€” Tabulation

Algorithm Steps:

  1. Use an array dp[] to store the minimum cost at each step.

  2. Base cases:

    • dp[0] = cost[0]

    • dp[1] = cost[1]

  3. Recurrence relation: $[ \text{dp[i]} = \text{cost[i]} + \min(\text{dp[i-1]}, \text{dp[i-2]}) $]

βœ… Time Complexity: O(N) βœ… Space Complexity: O(N)

2️⃣ Recursive + Memoization (O(N) Time, O(N) Space)

Algorithm Steps:

  1. Use recursion with memoization to avoid repeated calculations.

  2. Base cases:

    • If i < 0, return 0

    • If i == 0 || i == 1, return cost[i]

  3. Store computed results in a dp[] array to prevent redundant calls.

βœ… Time Complexity: O(N) βœ… Space Complexity: O(N)

3️⃣ Iterative Approach Without Extra Variables (O(N) Time, O(1) Space)

Algorithm Steps:

  1. Modify the input array cost[] in-place to store the cumulative minimum cost.

βœ… Time Complexity: O(N) βœ… Space Complexity: O(1)

Comparison of Approaches

Approach

⏱️ Time Complexity

πŸ—‚οΈ Space Complexity

βœ… Pros

⚠️ Cons

Iterative DP (Space Optimized)

🟑 O(N)

🟒 O(1)

Simple and fastest iterative method

Limited to Fibonacci-like logic

Dynamic Programming (Tabulation)

🟑 O(N)

πŸ”΄ O(N)

Easy to understand and implement

Consumes extra space

Recursive + Memoization

🟑 O(N)

πŸ”΄ O(N)

Natural recursive logic

Higher recursion overhead

Iterative In-Place Update

🟑 O(N)

🟒 O(1)

No extra variables used

Modifies input array

πŸ’‘ Best Choice?

βœ… For simplicity and efficiency: Use Iterative DP (Space Optimized). βœ… For an alternative approach without extra space: Use In-Place Iterative DP. βœ… For understanding step-by-step execution: Use Tabulation DP.

Code (Java)

Code (Python)

🎯 Contribution and Support:

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


πŸ“Visitor Count

Last updated