📖 Scenario: Imagine you want to climb a staircase with a certain number of 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, where you can take either 1 or 2 steps at a time.
📋 What You'll Learn
Create a variable n to represent the total number of steps in the staircase.
Create a variable ways to store the number of ways to climb each step.
Use a loop to calculate the number of ways to reach each step from 1 to n.
Print the total number of ways to climb n steps.
💡 Why This Matters
🌍 Real World
This problem models real-life situations where you have multiple ways to reach a goal by taking small steps, like climbing stairs or completing tasks in stages.
💼 Career
Understanding this problem helps in learning dynamic programming, a key skill for software engineers working on optimization and algorithm design.
Progress0 / 4 steps
1
Set the number of steps
Create a variable called n and set it to 5 to represent the total number of steps in the staircase.
DSA Python
Hint
Think of n as the height of the staircase you want to climb.
2
Initialize ways to climb steps
Create a list called ways with n + 1 elements, where ways[0] is 1 and ways[1] is 1. This list will store the number of ways to reach each step.
DSA Python
Hint
Use a list to keep track of the number of ways to reach each step. Start with 1 way to stand at step 0 and 1 way to reach step 1.
3
Calculate ways to climb each step
Use a for loop with variable i from 2 to n inclusive. Inside the loop, set ways[i] to ways[i - 1] + ways[i - 2] to calculate the number of ways to reach step i.
DSA Python
Hint
Each step can be reached either from the previous step or the step before that, so add those two counts.
4
Print the total number of ways
Print the value of ways[n] to display the total number of ways to climb n steps.
DSA Python
Hint
The final answer is the number of ways to reach the top step n.
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 total number of steps climbed
B. The maximum number of steps you can climb at once
C. The minimum number of steps to reach the top
D. The number of distinct ways to reach the top
Solution
Step 1: Understand the problem statement
The problem asks for the count of different ways to climb to the top using 1 or 2 steps at a time.
Step 2: Identify what is being asked
We want the total number of distinct sequences of 1-step and 2-step moves that sum to n.
Final Answer:
The number of distinct ways to reach the top -> Option D
Quick Check:
Count ways to climb = number of sequences [OK]
Hint: Count sequences of 1 or 2 steps to reach n [OK]
Common Mistakes:
Confusing number of ways with minimum steps
Thinking about maximum steps instead of ways
Counting total steps instead of sequences
2. Which of the following Python function signatures correctly represents a function to solve the Climbing Stairs problem?
easy
A. def climb_stairs(n: int) -> int:
B. def climb_stairs(n): return str
C. def climb_stairs(n: str) -> int:
D. def climb_stairs() -> int:
Solution
Step 1: Check parameter and return types
The input n is an integer representing steps, and output is an integer count of ways.
Step 2: Match function signature
def climb_stairs(n: int) -> int: correctly uses int input and int output with type hints.
Final Answer:
def climb_stairs(n: int) -> int: -> Option A
Quick Check:
Input int, output int function signature [OK]
Hint: Input is int steps, output is int ways [OK]
Common Mistakes:
Using string type for input
Returning string instead of int
Missing input parameter
3. What is the output of this code?
def climb_stairs(n: int) -> int:
if n <= 2:
return n
a, b = 1, 2
for _ in range(3, n + 1):
a, b = b, a + b
return b
print(climb_stairs(5))
medium
A. 5
B. 8
C. 13
D. 10
Solution
Step 1: Trace initial values and base cases
For n=5, start with a=1, b=2. Loop runs for i=3 to 5.
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 modification to the standard climbing stairs solution is correct?
hard
A. Use dp[i] = dp[i-1] * dp[i-2] * dp[i-3] with base dp[0]=1, dp[1]=1, dp[2]=2
B. Use dp[i] = dp[i-1] + dp[i-2] with base dp[0]=1, dp[1]=1
C. Use dp[i] = dp[i-1] + dp[i-2] + dp[i-3] with base dp[0]=1, dp[1]=1, dp[2]=2
D. Use dp[i] = dp[i-1] + dp[i-3] with base dp[0]=1, dp[1]=1, dp[2]=2
Solution
Step 1: Understand step options and recurrence
With steps 1, 2, or 3 allowed, ways to reach i = sum of ways to reach i-1, i-2, and i-3.
Step 2: Set correct base cases
dp[0]=1 (one way to stand still), dp[1]=1, dp[2]=2 (1+1 or 2 steps).
Final Answer:
Use dp[i] = dp[i-1] + dp[i-2] + dp[i-3] with base dp[0]=1, dp[1]=1, dp[2]=2 -> Option C
Quick Check:
Sum previous three dp values for 3-step climb [OK]
Hint: Sum ways from last 3 steps for 1-2-3 step climb [OK]
Common Mistakes:
Multiplying dp values instead of adding
Using only two previous dp values
Incorrect base cases
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.