fbaquant / LeetCode

1 stars 0 forks source link

Climbing Stairs #3

Open fbaquant opened 1 year ago

fbaquant commented 1 year ago

https://leetcode.com/problems/climbing-stairs

fbaquant commented 1 year ago
class Solution:

    def climbStairs(self, n: int) -> int:
        if n == 1:
            return 1
        elif n == 2:
            return 2
        a = 1
        b = 2
        for _ in range(n-2):
            c = b + a
            a = b
            b = c
        return c
fbaquant commented 1 year ago
import functools

class Solution:
    @functools.lru_cache()
    def climbStairs(self, n: int) -> int:
        return self.climbStairs(n - 1) + self.climbStairs(n - 2) if n > 2 else 2 if n == 2 else 1