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).
- If
nis less than or equal to 2, returnn. - Initialize two variables,
step1andstep2, to represent the ways to reach the first and second stairs (1 and 2). - Loop from the 3rd stair up to
n:- Calculate
current = step1 + step2. - Update
step1andstep2for the next stair.
- Calculate
- Print the final result.
01EXAMPLE 1
Input
n=3Output
3Explanation: Ways: [1,1,1], [1,2], [2,1].
Constraints
- O(n) time complexity.
AlgorithmsDynamic Programming
JavaScriptSystem handles I/O — write your function only
Loading...
1 Hidden
Input Arguments
n3
Expected Output
3
Click RUN to test your solution