Leetcode 1137. N-th Tribonacci Number Python3 풀이

SeungMinLee·2022년 4월 4일
0
post-thumbnail

Dynamic Programming Day 1.

class Solution(object):
    def tribonacci(self, n):
        """
        :type n: int
        :rtype: int
        """
        tribo = [0 for _ in range(n+1)]
        if n >= 1:
            tribo[1] = 1
        if n >= 2:
            tribo[2] = 1

        for _ in range(3, n+1):
            tribo[_] = tribo[_-3] + tribo[_-2] + tribo[_-1]

        return tribo[n]

메모이제이션을 이용하여 해결한다.

profile
학생 개발자

0개의 댓글