DEV Community

Discussion on: Leetcode - Climbing Stairs (with JavaScript)

Collapse
 
evgenyartemov profile image
Evgeny Artemov

Could you provide me with some further explanations, why do you start i from 3 ?

Collapse
 
maparr profile image
Maksym Parfenov • Edited

hi
because for the first iteration where n = 1 we go to the first if and return 1
because for 1 step we have only 1 way
second iteration is also defined and return second is 2, because for 2 steps, we have also only 1 way, 1 step + 1 step
if n = 3 , we 're calculating only 1 iteration in for loop , and getting second = third which is result first + second , and we have 3 ways , 1 + 1, 1 + 2, 1 + 1 + 1 ,

other variants is about Fibonacci Fib(n) = Fib(n-1)+Fib(n-2)