Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Climbing Stairs Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Climbing Stairs

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

There is a staircase with n steps. You need to reach the top and you can either climb 1 step or 2 steps at a time. In how many distinct ways can you reach the top of the staircase.

Naive Approach

We can solve it naively with a recursive brute force approach. The number of ways to reach the ith stair is the sum of the number of ways to reach the stair i - 1 and stair i - 2.

Analysis

  • Time Complexity: O(2n)
  • Space Complexity: O(n)

Implementation

C++
int recursiveStairs(int n) {
	if(n == 1) {
		return 1;
	}
	else if(n == 2) {
		return 2;
	}
	return recursiveStairs(n - 1) + recursiveStairs(n - 2);
}
int climbStairs(int n) {
    return recursiveStairs(n);
}
Java
class Solution {
	int recursiveStairs(int n) {
		if(n == 1) {
			return 1;
		}
		else if(n == 2) {
			return 2;
		}
		return recursiveStairs(n - 1) + recursiveStairs(n - 2);
	}
	int climbStairs(int n) {
	    return recursiveStairs(n);
	}
}

Optimal Approach

We can observe in this problem, that multiple function calls get called over and over again redundantly here, i.e there is some overlapping subproblem here. This hints at the use of DP to memoize the brute recursion and improved the solution to linear time.

Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

Implementation

C++
int dp[50];
int recursiveStairs(int n) {
	if(n == 1) {
		return 1;
	}
	else if(n == 2) {
		return 2;
	}
	else if(dp[n] != - 1) {
		return dp[n];
	}
	return dp[n] = recursiveStairs(n - 1) + recursiveStairs(n - 2);
}
int climbStairs(int n) {
	memset(dp, -1, sizeof(dp));
    return recursiveStairs(n);
}
Java
class Solution {
	int dp[] = new int[50];
	int recursiveStairs(int n) {
		if(n == 1) {
			return 1;
		}
		else if(n == 2) {
			return 2;
		}
		else if(dp[n] != -1) {
			return dp[n];
		}
		return dp[n] = recursiveStairs(n - 1) + recursiveStairs(n - 2);
	}
	int climbStairs(int n) {
		for(int i = 0; i <= n; i++) {
			dp[i] = -1;
 		}
	    return recursiveStairs(n);
	}
}

Bottom Up Implementation

C++
vector<int> dp;
int climbStairs(int n) {
	if(n == 1) {
		return 1;
	} 
	dp.resize(n + 1);
	dp[1] = 1;
	dp[2] = 2;
	for(int i = 3; i <= n; i++) {
		dp[i] = dp[i - 1] + dp[i - 2];
	}
    return dp[n];
}
Java
class Solution {
	int dp[];
	int climbStairs(int n) {
		if(n == 1) {
		return 1;
		} 
		dp = new int[n + 1];
		dp[1] = 1;
		dp[2] = 2;
		for(int i = 3; i <= n; i++) {
			dp[i] = dp[i - 1] + dp[i - 2];
		}
		return dp[n];
	}
}
Related Content
Best Time to Buy and Sell Stocks
Best Time to Buy and Sell Stocks II
Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock IV
Coin Change
Collect Jewels
Decode Ways
Edit Distance (Levenshtein Distance)
Minimum Egg Drops
Longest Common Subsequence (LCS)
Longest Increasing Subsequence (LIS)
Longest Palindromic Substring (LPS)
Maximum path sum in matrix
Maximum Product Subarray
Maximum Sum Increasing Subsequence
Palindrome Partitioning
Palindrome Partitioning 2
Regular Expression Matching
Rod Cutting
Subset Sum
Subset Sum 2
Trapping Rain Water
Unique Paths
Wildcard Matching
Word Break
Word Break - II
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet