📖 Scenario: Imagine you want to climb a staircase with n steps. You can climb either 1 step or 2 steps at a time. You want to find out how many different ways you can reach the top.
🎯 Goal: Build a program that calculates the number of distinct ways to climb to the top of a staircase with n steps, using steps of size 1 or 2.
📋 What You'll Learn
Create an integer variable n with the exact value 5
Create an integer array dp of size n + 1 to store ways to climb
Use a for loop with variable i from 2 to n inclusive
Calculate dp[i] as the sum of dp[i - 1] and dp[i - 2]
Print the value of dp[n] as the final output
💡 Why This Matters
🌍 Real World
Counting ways to climb stairs is like counting different paths or combinations in real life, such as planning routes or steps in a process.
💼 Career
This problem teaches dynamic programming basics, which is useful in software development, especially in optimization and algorithm design.
Progress0 / 4 steps
1
Set the number of steps
Create an integer variable called n and set it to 5.
DSA C++
Hint
Use int n = 5; to store the number of steps.
2
Create the ways array
Create an integer array called dp of size n + 1. Initialize dp[0] to 1 and dp[1] to 1.
DSA C++
Hint
Use int dp[6]; because n + 1 = 6. Set dp[0] and dp[1] to 1 because there is 1 way to stand still or take one step.
3
Calculate ways to climb
Use a for loop with variable i from 2 to n inclusive. Inside the loop, set dp[i] to dp[i - 1] + dp[i - 2].
DSA C++
Hint
Use the formula dp[i] = dp[i - 1] + dp[i - 2] to count ways to reach step i.
4
Print the result
Print the value of dp[n] using std::cout.
DSA C++
Hint
Use std::cout << dp[n] << std::endl; to print the number of ways.
Practice
(1/5)
1. What does the Climbing Stairs problem ask you to find? Given n steps, how many distinct ways can you climb to the top if you can take 1 or 2 steps at a time?
easy
A. The maximum number of steps you can take at once
B. The total number of distinct ways to reach the top
C. The minimum number of steps to reach the top
D. The number of steps you skip
Solution
Step 1: Understand the problem statement
The problem asks for the count of different ways to climb n steps taking 1 or 2 steps at a time.
Step 2: Identify what is being calculated
We want the total number of distinct sequences of 1-step and 2-step moves that sum to n.
Final Answer:
The total number of distinct ways to reach the top -> Option B
Quick Check:
Count ways to climb = total distinct sequences [OK]
Hint: Count sequences of 1 or 2 steps summing to n [OK]
Common Mistakes:
Confusing number of ways with minimum steps
Thinking about maximum steps instead of counting ways
Ignoring the step size constraints
2. Which of the following is the correct C++ function signature to solve the Climbing Stairs problem recursively?
easy
A. int climbStairs(int &n);
B. void climbStairs(int n);
C. int climbStairs(int n);
D. int climbStairs();
Solution
Step 1: Identify the return type
The function should return an integer count of ways, so return type is int.
Step 2: Check the parameter list
We need the number of steps n as input, passed by value as int n.
Final Answer:
int climbStairs(int n); -> Option C
Quick Check:
Return type int and parameter int n [OK]
Hint: Function returns int count and takes int n parameter [OK]
Common Mistakes:
Using void return type instead of int
Missing the parameter n
Passing n by reference unnecessarily
3. What is the output of the following C++ code?
int climbStairs(int n) {
if (n <= 2) return n;
return climbStairs(n-1) + climbStairs(n-2);
}
int main() {
std::cout << climbStairs(4);
return 0;
}
Hint: Use recursion formula: ways(n) = ways(n-1) + ways(n-2) [OK]
Common Mistakes:
Returning n instead of sum for n > 2
Miscounting base cases
Confusing return values for climbStairs(3)
4. Find the error in this C++ code for climbing stairs:
int climbStairs(int n) {
if (n == 1) return 1;
if (n == 2) return 2;
return climbStairs(n-1) * climbStairs(n-2);
}
medium
A. Using multiplication instead of addition in recursion
B. Base cases are incorrect
C. Missing return type
D. Function parameter should be passed by reference
Solution
Step 1: Check base cases
Base cases for n=1 and n=2 are correct returning 1 and 2 respectively.
Step 2: Analyze recursive return statement
It uses multiplication (*) instead of addition (+), which is wrong for counting ways.
Final Answer:
Using multiplication instead of addition in recursion -> Option A
Quick Check:
Recursion sums ways, not multiplies [OK]
Hint: Sum ways from previous two steps, don't multiply [OK]
Common Mistakes:
Multiplying instead of adding recursive calls
Incorrect base cases for n=1 or n=2
Passing parameter by reference unnecessarily
5. You want to find the number of ways to climb 10 steps but can take 1, 2, or 3 steps at a time. Which approach correctly adapts the Climbing Stairs problem?
hard
A. Use dp[n] = dp[n-1] + dp[n-2] + dp[n-3] with base dp[0]=1, dp[1]=1, dp[2]=2
B. Use dp[n] = dp[n-1] * dp[n-2] * dp[n-3] with base dp[0]=0, dp[1]=1, dp[2]=1
C. Use recursion with only dp[n] = dp[n-1] + dp[n-2]
D. Use dp[n] = dp[n-1] + dp[n-3] ignoring dp[n-2]
Solution
Step 1: Understand the step options
Since steps can be 1, 2, or 3, ways to reach step n = sum of ways to reach n-1, n-2, and n-3.
Step 2: Define base cases and recurrence
Base cases: dp[0]=1 (one way to stand still), dp[1]=1, dp[2]=2. Recurrence: dp[n] = dp[n-1] + dp[n-2] + dp[n-3].
Final Answer:
Use dp[n] = dp[n-1] + dp[n-2] + dp[n-3] with base dp[0]=1, dp[1]=1, dp[2]=2 -> Option A
Quick Check:
Sum ways from last 3 steps with correct bases [OK]
Hint: Add ways from last 3 steps for 1,2,3 step moves [OK]
Common Mistakes:
Multiplying ways instead of adding
Ignoring dp[n-3] term
Wrong base cases causing incorrect counts
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.