70. Climbing Stairs
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
Method: Dynamic Programming
The problem asks for the number of distinct ways to climb a staircase of n steps, where one can climb either 1 or 2 steps at a time. This is a classic dynamic programming problem.
Solution
Climbing Stairs in Python
class Solution:
def climbStairs(self, n: int) -> int:
# The problem can be solved using dynamic programming. The number of ways
# to reach step n, let's call it ways(n), is the sum of ways to reach
# step n-1 and ways to reach step n-2. This is because you can take either
# a 1-step or a 2-step jump to reach n.
# So, ways(n) = ways(n-1) + ways(n-2), which is the Fibonacci sequence.
# Base cases:
# ways(1) = 1
# ways(2) = 2
if n <= 2:
return n
# We can optimize space by only keeping track of the last two values.
# two_steps_back corresponds to ways(i-2)
# one_step_back corresponds to ways(i-1)
two_steps_back = 1
one_step_back = 2
# Iterate from 3 to n to calculate ways for each step.
for i in range(3, n + 1):
current_ways = one_step_back + two_steps_back
# Update for the next iteration
two_steps_back = one_step_back
one_step_back = current_ways
return one_step_back
Climbing Stairs in Java
class Solution {
public int climbStairs(int n) {
// Base cases: if n is 1 or 2, the number of ways is n itself.
if (n <= 2) {
return n;
}
// This is a classic dynamic programming problem, which is equivalent
// to finding the (n+1)-th Fibonacci number.
// We can solve it iteratively with constant space.
// two_steps_back represents the number of ways to reach step i-2.
// Initialize with ways to reach step 1.
int two_steps_back = 1;
// one_step_back represents the number of ways to reach step i-1.
// Initialize with ways to reach step 2.
int one_step_back = 2;
// Iterate from step 3 to n.
for (int i = 3; i <= n; i++) {
// The number of ways to reach the current step 'i' is the sum
// of ways to reach i-1 and i-2.
int current_ways = one_step_back + two_steps_back;
// Update the variables for the next iteration.
two_steps_back = one_step_back;
one_step_back = current_ways;
}
// one_step_back now holds the number of ways to reach step n.
return one_step_back;
}
}
Climbing Stairs in C++
class Solution {
public:
int climbStairs(int n) {
// Base cases: if n is 1 or 2, the number of ways is n itself.
if (n <= 2) {
return n;
}
// This is a classic dynamic programming problem, which is equivalent
// to finding the (n+1)-th Fibonacci number.
// We can solve it iteratively with constant space.
// two_steps_back represents the number of ways to reach step i-2.
// Initialize with ways to reach step 1.
int two_steps_back = 1;
// one_step_back represents the number of ways to reach step i-1.
// Initialize with ways to reach step 2.
int one_step_back = 2;
// Iterate from step 3 to n.
for (int i = 3; i <= n; i++) {
// The number of ways to reach the current step 'i' is the sum
// of ways to reach i-1 and i-2.
int current_ways = one_step_back + two_steps_back;
// Update the variables for the next iteration.
two_steps_back = one_step_back;
one_step_back = current_ways;
}
// one_step_back now holds the number of ways to reach step n.
return one_step_back;
}
};
Climbing Stairs in TypeScript
function climbStairs(n: number): number {
// Base cases: if n is 1 or 2, the number of ways is n itself.
if (n <= 2) {
return n;
}
// This is a classic dynamic programming problem, which is equivalent
// to finding the (n+1)-th Fibonacci number.
// We can solve it iteratively with constant space.
// 'two_steps_back' represents the number of ways to reach step i-2.
// Initialize with ways to reach step 1.
let two_steps_back = 1;
// 'one_step_back' represents the number of ways to reach step i-1.
// Initialize with ways to reach step 2.
let one_step_back = 2;
// Iterate from step 3 to n.
for (let i = 3; i <= n; i++) {
// The number of ways to reach the current step 'i' is the sum
// of ways to reach i-1 and i-2.
const current_ways = one_step_back + two_steps_back;
// Update the variables for the next iteration.
two_steps_back = one_step_back;
one_step_back = current_ways;
}
// 'one_step_back' now holds the number of ways to reach step n.
return one_step_back;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.