Climbing Stairs

IF
AlgoAxiomStaff Engineers
JSTS
Easy20 mins

You are climbing a staircase. It takes n steps to reach the top.

Each time you can either climb 1 or 2 steps. Return the number of distinct ways you can climb to the top.

Examples

Example 1:

Input: n = 2

Output: 2

Explanation: There are two ways to climb to the top: 1 + 1 and 2.

Example 2:

Input: n = 3

Output: 3

Explanation: There are three ways to climb to the top: 1 + 1 + 1, 1 + 2, and 2 + 1.

Example 3:

Input: n = 5

Output: 8

Explanation: The 8 distinct ways include combinations like 1+1+1+1+1, 2+1+1+1, 1+2+2, etc.

Constraints

  • 1 <= n <= 45
Source: Dynamic Programming pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle