πŸš€Day 8. Ways to Reach the n'th Stair 🧠

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

πŸ’‘ Problem Description:

There are n stairs, and a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Your task is to count the number of ways the person can reach the top (order matters).

πŸ” Example Walkthrough:

Example 1:

Input:

n = 1

Output:

1

Explanation:

There is only one way to climb 1 stair β€” just one step of size 1.

Example 2:

Input:

Output:

Explanation:

There are two ways to reach the 2nd stair:

  1. (1, 1)

  2. (2)

Example 3:

Input:

Output:

Explanation:

There are five ways to climb 4 stairs:

  1. (1, 1, 1, 1)

  2. (1, 1, 2)

  3. (1, 2, 1)

  4. (2, 1, 1)

  5. (2, 2)

Constraints:

  • $(1 \le n \le 44)$

🎯 My Approach:

Iterative (Space-Optimized DP)

  1. We know this is essentially the Fibonacci sequence shifted by one index.

  2. Use two variables a and b to track the ways to climb (n-1) and n stairs.

  3. Initially, a = 1 and b = 1, representing ways to climb 0 or 1 stairs.

  4. Update in a loop from 2 to n, computing b = a + b (the sum of ways to climb (n-1) and (n-2)), then shift a to the old b.

This approach eliminates the need for an entire DP array and uses only constant space.

πŸ•’ Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n), as we iterate from 2 to n once.

  • Expected Auxiliary Space Complexity: O(1), since we only use a constant amount of extra space (two variables).

πŸ“ Solution Code

Code (C++)

⚑ Alternative Approaches

1️⃣ Iterative DP (O(N) Time, O(1) Space) β€” Efficient and Clean

Algorithm Steps:

  • Maintain two variables, a and b, initialized to 1.

  • Iterate from 2 to n, updating b as a + b and shifting a to the old value of b.

  • This efficient method eliminates the need for an array.

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

2️⃣ Dynamic Programming (Tabulation - O(N) Time, O(N) Space)

Algorithm Steps:

  • Create an array dp[] where dp[i] stores the number of ways to reach the i-th step.

  • Base cases:

    • dp[0] = 1 (1 way to stay at the ground).

    • dp[1] = 1 (1 way to take one step).

  • Recurrence relation: $[ \text{dp[i]} = \text{dp[i-1]} + \text{dp[i-2]} ]$

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

3️⃣ Matrix Exponentiation (O(log N) Time, O(1) Space) β€” Fastest Approach

Algorithm Steps:

  • Fibonacci sequence can be efficiently calculated using matrix exponentiation.

  • Matrix multiplication transforms the problem into logarithmic complexity.

Matrix Representation:

F(n)
F(n-1)

F(n-1)

F(n-2)

=

1
1

1

0

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

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

Algorithm Steps:

  • Use recursion with memoization to store previously computed results.

  • Base cases:

    • f(0) = 1

    • f(1) = 1

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

πŸ“Š 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 logic only

Dynamic Programming (Tabulation)

🟑 O(N)

πŸ”΄ O(N)

Easy to understand and implement

Consumes more space

Matrix Exponentiation

🟒 O(log N)

🟒 O(1)

Fastest for large values of n

Slightly complex logic

Recursive + Memoization

🟑 O(N)

πŸ”΄ O(N)

Natural recursive logic

Higher recursion overhead

πŸ’‘ Best Choice?

βœ… For simplicity and efficiency: Use Iterative DP (Space Optimized). βœ… For fastest results in large inputs: Use Matrix Exponentiation. βœ… For easier implementation with clear logic: Use Tabulation.

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