Climbing Stairs Solution for Accenture

Company: Accenture · Difficulty: Easy · Topics: Dynamic Programming, Math

Problem Statement

You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Key Understanding:

Examples

Example 1:

Input: n = 2

Output: 2

Explanation:

1. 1 step + 1 step = [1,1]

2. 2 steps = [2]

```

Step 0 → Step 1 → Step 2

| | |

| [1,1] [2]

| | |

| [1,2] [2]

```

Example 2:

Input: n = 3

Output: 3

Explanation:

1. 1 step + 1 step + 1 step = [1,1,1]

2. 1 step + 2 steps = [1,2]

3. 2 steps + 1 step = [2,1]

```

Step 0 → Step 1 → Step 2 → Step 3

| | | |

| [1,1] [1,1,1] [1,2]

| | | |

| [2] [2,1] [2]

```

Example 3:

Input: n = 4

Output: 5

Explanation:

1. 1+1+1+1 = [1,1,1,1]

2. 1+1+2 = [1,1,2]

3. 1+2+1 = [1,2,1]

4. 2+1+1 = [2,1,1]

5. 2+2 = [2,2]

Example 4:

Input: n = 5

Output: 8

Explanation:

1. [1,1,1,1,1]

2. [1,1,1,2]

3. [1,1,2,1]

4. [1,2,1,1]

5. [1,2,2]

6. [2,1,1,1]

7. [2,1,2]

8. [2,2,1]

Intuition

The key insight is that this is a Fibonacci sequence problem. The number of ways to climb n steps is equal to the sum of ways to climb (n-1) steps and (n-2) steps.

Core Concept:

Why This Works:

Mathematical Insight:

Pattern Recognition:

Approach

Approach 1: Dynamic Programming (Optimal)

Algorithm:

1. Initialize two variables: prev = 1, curr = 2

2. For i from 3 to n:

3. Return curr

Why This Works:

Complexity Analysis:

Approach 2: Recursion with Memoization

Algorithm:

1. Create a memoization array to store computed values

2. Define recursive function ways(n):

Why This Works:

Complexity Analysis:

Approach 3: Matrix Exponentiation

Algorithm:

1. Use matrix exponentiation to find Fibonacci numbers

2. The Fibonacci sequence can be represented as matrix multiplication

3. Use fast exponentiation for O(log n) solution

Why This Works:

Complexity Analysis:

Dry Run

Let's trace through Example 1: n = 2

Dynamic Programming Approach:

Step 1: Initialize

Step 2: For n = 2

Result: 2 ways to climb 2 steps

Let's trace through Example 2: n = 3

Step 1: Initialize

Step 2: For i = 3

Step 3: Return curr = 3

Result: 3 ways to climb 3 steps

Let's trace through Example 3: n = 4

Step 1: Initialize

Step 2: For i = 3

Step 3: For i = 4

Step 4: Return curr = 5

Result: 5 ways to climb 4 steps

Recursion with Memoization Approach:

Step 1: Create memo array

Step 2: Calculate ways(4)

Result: 5 ways to climb 4 steps

Key Insights

1. Fibonacci Pattern: The solution follows the Fibonacci sequence

2. Optimal Substructure: The problem can be broken down into smaller subproblems

3. Overlapping Subproblems: The same subproblems are solved multiple times

4. Constant Space: We only need the last two values to calculate the next one

5. Mathematical Elegance: The solution is mathematically beautiful and efficient

Edge Cases

1. n = 1: Return 1 (only one way: take 1 step)

2. n = 2: Return 2 (two ways: [1,1] or [2])

3. n = 0: Not valid input (constraints specify n ≥ 1)

4. Large n: Algorithm handles large values efficiently

5. Overflow: For very large n, consider using long long or modular arithmetic

Solution

⚠️ Disclaimer: Don't jump directly to the solution, try it out yourself first. Only look at the solution after you've given it a good attempt.

Solution 1: Dynamic Programming (Optimal)

Time Complexity: O(n) - We compute each value once

Space Complexity: O(1) - We use only constant extra space

Advantages:

Disadvantages:

Solution 2: Recursion with Memoization

Time Complexity: O(n) - Each value computed once

Space Complexity: O(n) - Due to recursion stack and memoization

Advantages:

Disadvantages:

Solution 3: Matrix Exponentiation

Time Complexity: O(log n) - Matrix exponentiation

Space Complexity: O(1) - Constant space for matrix operations

Advantages:

Disadvantages:

More Accenture coding questions