Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Count Ways To Reach Nth Stair

Medium Acceptance 60.00% Points 30.00

You are given an integer N representing the number of stairs. You can climb either 1 step or 2 steps at a time. Your task is to count the total number of different ways to reach exactly the Nth stair.

Calculate and return the total number of possible ways.

Examples
Example 1

Example 1

Input: N = 5

Output: 8

Explanation: The possible ways are (1+1+1+1+1), (1+1+1+2), (1+1+2+1), (1+2+1+1), (2+1+1+1), (1+2+2), (2+1+2), and (2+2+1).

Example 2

Example 2

Input: N = 6

Output: 13

Explanation: The possible ways are (1+1+1+1+1+1), (1+1+1+1+2), (1+1+1+2+1), (1+1+2+1+1), (1+2+1+1+1), (2+1+1+1+1), (1+1+2+2), (1+2+1+2), (1+2+2+1), (2+1+1+2), (2+1+2+1), (2+2+1+1), and (2+2+2).

Example 3

Example 3

Input: N = 4

Output: 5

Explanation: The possible ways are (1+1+1+1), (1+1+2), (1+2+1), (2+1+1), and (2+2).

Hints
Hint 1
N/A
Constraints
  • 1 = N = 30
Companies
Amazon
Topics
Recursion
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions