Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

70. Climbing Stairs

Easy53.2% Acceptance
MathDynamic ProgrammingMemoization
Asked by:
A
Amazon
ProblemHints (1)Solutions (12)VideosCompanies (9)Notes

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?

Example 1:

Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps

Example 2:

Input: n = 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step

Constraints:

  • 1 <= n <= 45
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
E
Expedia
M
Microsoft
U
Uber
G
Google
+4

Approach

The key idea behind #70 Climbing Stairs is recognizing that the number of ways to reach step n depends on how you reached the previous steps. From any step, you can either climb 1 or 2 stairs. This leads to the recurrence relation: the ways to reach step n equals the sum of ways to reach n-1 and n-2. This pattern closely resembles the Fibonacci sequence.

A straightforward recursive solution exists but results in repeated computations, making it inefficient. To optimize this, you can use memoization (top-down dynamic programming) to store previously computed results and avoid recomputation. Another approach is bottom-up dynamic programming, where you iteratively build solutions from smaller steps up to n.

For further optimization, since only the last two states are required, the DP array can be reduced to two variables, achieving O(1) space complexity while maintaining O(n) time complexity.

Complexity

ApproachTime ComplexitySpace Complexity
Naive RecursionO(2^n)O(n)
Dynamic Programming (Memoization / Tabulation)O(n)O(n)
Space Optimized DPO(n)O(1)

Video Solution Available

NeetCode

View all video solutions

Problem Hints

Use these hints if you're stuck. Try solving on your own first.

1
Hint 1

To reach nth step, what could have been your previous steps? (Think about the step sizes)

Ready to see the solutions?View Solutions

Solutions (12)

Recursive Approach with Memoization

This approach uses recursion to explore the different ways to reach the top. To optimize, we use memoization to store results of subproblems to avoid redundant calculations. This ensures that each subproblem is solved only once, dramatically improving efficiency.

Time Complexity: O(n), as each state is only computed once.
Space Complexity: O(n), due to the memoization array.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#define MAX 46
3
4int memo[MAX] = {0};
5
6int climbStairs(int n) {
7    if (n == 1) return 1;
8    if (n == 2) return 2;
9    if (memo[n] != 0) return memo[n];
10    memo[n] = climbStairs(n - 1) + climbStairs(n - 2);
11    return memo[n];
12}
13
14int main() {
15    int n = 3;
16    printf("%d", climbStairs(n));
17    return 0;
18}

Explanation

This C code provides a recursive solution enhanced with memoization. We define an array memo to store the results of previously solved subproblems. This helps in reducing redundant calculations, thus optimizing the recursive solution.

Dynamic Programming Approach

This approach builds from the base up using a table to store results at each step. Starting with known base cases, each subsequent result is built by combining previous results. This eliminates the recursive overhead, making it a very efficient algorithm.

Time Complexity: O(n), since each value is calculated sequentially in a loop.
Space Complexity: O(n), for storing the results in the dp array.

CC++JavaPythonC#JavaScript
1#

Video Solutions

Watch expert explanations and walkthroughs

Climbing Stairs - Dynamic Programming - Leetcode 70 - Python

NeetCode
18:08721,900 views

Asked By Companies

9 companies
A
Amazon
E
Expedia
M
Microsoft
U
Uber
G
Google
A
Adobe
Y
Yahoo
G
Goldman Sachs
B
Bloomberg

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Unique PathsMedium
Unique Binary Search TreesMedium
Number of Digit OneHard
Different Ways to Add ParenthesesMedium
More similar problems

Related Topics

MathDynamic ProgrammingMemoization

Problem Stats

Acceptance Rate53.2%
DifficultyEasy
Companies9

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Climbing Stairs asked in FAANG interviews?

Yes, Climbing Stairs is a very common beginner-level interview problem at companies like Google, Amazon, and Meta. It tests a candidate’s understanding of dynamic programming and recognizing Fibonacci-like patterns.

What data structure is best for Climbing Stairs?

Dynamic programming structures such as arrays or simple variables are typically used. In memoization, a hash map or array stores previously computed results to avoid redundant recursion.

What is the optimal approach for Climbing Stairs?

The optimal approach uses dynamic programming based on the Fibonacci pattern. Each step depends on the number of ways to reach the previous two steps. A space-optimized DP solution achieves O(n) time and O(1) space complexity.

Why is Climbing Stairs related to the Fibonacci sequence?

The number of ways to reach step n equals the sum of ways to reach steps n-1 and n-2. This recurrence relation is identical to the Fibonacci sequence, making the problem a classic DP example.

Previous Problem

Sqrt(x)

Next Problem

Gray Code

include
<stdio.h>
2
3
int
climbStairs
(
int
n
)
{
4
if
(
n
<=
2
)
return
n
;
5
int
dp
[
n
+
1
]
;
6
dp
[
1
]
=
1
;
7
dp
[
2
]
=
2
;
8
for
(
int
i
=
3
;
i
<=
n
;
++
i
)
{
9
dp
[
i
]
=
dp
[
i
-
1
]
+
dp
[
i
-
2
]
;
10
}
11
return
dp
[
n
]
;
12
}
13
14
int
main
(
)
{
15
int
n
=
3
;
16
printf
(
"%d"
,
climbStairs
(
n
)
)
;
17
return
0
;
18
}

Explanation

This C code uses dynamic programming to solve the problem iteratively with a dp array. It starts with known results for 1 step and 2 steps and iteratively computes the number of ways for all steps up to n, reducing the time complexity compared to a naive recursive approach.