70. Climbing Stairs

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

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?


It is indeed Fibonacci series!

Because ways(n) = ways(n-2) + ways(n-1)


def climbStairs(n):
   
    if n <= 2:
        return n
   
    first = 1
    second = 2        
    i = 2
   
    while i < n:            
        cur = first + second
        first = second
        second = cur
        i += 1
       

    return second 

Comments

Popular posts from this blog

347. Top K Frequent Elements

849. Maximize Distance to Closest Person

674. Longest Continuous Increasing Subsequence