← switch

509. Fibonacci Number

easy

Math · Dynamic Programming · Recursion · Memoization

read the problem on leetcode →

first solved 18 Jun 2023

python
class Solution(object):
    def fib(self, n):
        """
        :type n: int
        :rtype: int
        """
        if n==0:
            return 0
        dp=[0]* (n+1)
        dp[0]=0
        dp[1]=1

        for i in range(2, n+1):
            dp[i]=dp[i-1]+dp[i-2]
        
        return dp[n]