개발공부/algorithm

[Leetcode][python] 1137. N-th Tribonacci Number - DP

so.py 2021. 5. 16. 21:32

Problem

  • Leetcode 1137. N-th Tribonacci Number - DP
  • Level: Easy
  • Link
 

N-th Tribonacci Number - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

My Approach

Classic fibonacci problem with DP algorithm

class Solution:
    def tribonacci(self, n: int) -> int:
        arr = [0, 1, 1]
        if n < 3:
            return arr[n]
        
        for i in range(3, n + 1):
            nxt = arr[i - 1] + arr[i - 2] + arr[i - 3]
            arr.append(nxt)
        
        return arr[n]