Leetcode 70: Climbing Stairs

70. Climbing Stairs

  • Total Accepted: 139063
  • Total Submissions: 363734
  • Difficulty: Easy
  • Contributors: Admin

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

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

 

Code

from math import sqrt

class Solution(object):
    def climbStairs(self, n):
        """
        :type n: int
        :rtype: int
        """
        if n < 0:
            return -1
        elif n == 0:
            return 0
        else:
            return int((((1 + sqrt(5))/2)**(n+1) - ((1 - sqrt(5))/2)**(n+1))/sqrt(5))

 

Idea

Essentially a fibonacci sequence.

Leave a comment

Your email address will not be published. Required fields are marked *