Leetcode Problem 1137. N-th Tribonacci Number

1137. N-th Tribonacci Number

Leetcode Solutions

Approach: Better Dynamic Programming (Bottom Up)

  1. If n is less than 3, return the corresponding base case value (0, 1, or 1).
  2. Initialize three variables a, b, and c with the base cases: a = 0, b = 1, c = 1.
  3. Iterate from 3 to n, updating the values of a, b, and c to the next term in the sequence: temp = a + b + c, a = b, b = c, c = temp.
  4. After the loop, return the value of c, which now holds the nth Tribonacci number.
UML Thumbnail

Approach: Dynamic Programming (Top Down with Memoization)

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...