The Infinite Climb

EasyAcc. 95.4%
+20 XP 8

The Way of the Traveler (Climbing Stairs)

Imagine you are a traveler climbing a sacred staircase. You can either take 1 step or 2 steps at a time. Your mission is to find out how many distinct ways you can reach the top. Although it looks simple, the number of combinations grows exponentially—but by remembering your previous steps, you can calculate the result in record time!

The Assignment

Your mission is to find the total combinations. Your function receives a number n (the number of stairs).

  1. If n is less than or equal to 2, return n.
  2. Initialize two variables, step1 and step2, to represent the ways to reach the first and second stairs (1 and 2).
  3. Loop from the 3rd stair up to n:
    • Calculate current = step1 + step2.
    • Update step1 and step2 for the next stair.
  4. Print the final result.

01EXAMPLE 1

Inputn=3
Output3

Explanation: Ways: [1,1,1], [1,2], [2,1].

Constraints

  • O(n) time complexity.
AlgorithmsDynamic Programming
JavaScript
Loading...
1 Hidden

Input Arguments

n3

Expected Output

3

Click RUN to test your solution