Leetcode # 1137. N-th Tribonacci Number

https://leetcode.com/problems/n-th-tribonacci-number

Solution

Time Complexity: O(n)
Space Complexity: O(n)
(The input and output generally do not count towards the space complexity.)

class Solution:
  def __init__(self):
    self.tribonacci_table = {0: 0, 1: 1, 2: 1}
  def tribonacci(self, n: int) -> int:
    if n >= len(self.tribonacci_table):
      self.tribonacci_table[n] = self.tribonacci(n - 3) + self.tribonacci(n - 2) + self.tribonacci(n - 1)
    return self.tribonacci_table[n]

 

Last Updated on 2023/08/16 by A1go

目錄

目錄
Bitnami