Exceed Time Limit


  • 0
    S

    It says my running time exceeds. Does it mean that recursion is not accepted in Python?

    class Solution:
    # @param {integer} n
    # @return {integer}
    def climbStairs(self, n):
        Total = 0
        if n == 1:
            Total = 1
        elif n == 2:
            Total = 2
        else:
            Total = self.climbStairs(n-1) + self.climbStairs(n-2)
                
        return Total

  • 0
    Y

    You need to cache the result of each recursion. e.g. Store them in a array.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.