1137. N-th Tribonacci Number

The Tribonacci sequence Tn is defined as follows: 

T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.

Given n, return the value of Tn.

 

Example 1:

Input: n = 4
Output: 4
Explanation:
T_3 = 0 + 1 + 1 = 2
T_4 = 1 + 1 + 2 = 4

Example 2:

Input: n = 25
Output: 1389537

 

Constraints:

  • 0 <= n <= 37
  • The answer is guaranteed to fit within a 32-bit integer, ie. answer <= 2^31 - 1.

Solution


Possible Solutions: Space vs Performance Optimisation

There could be two approaches here. The first one is to optimise the performance, and the second one is to minimize the space used.

Let's start from the performance optimisation.

Since n is known to be less then 38, it's enough to precompute all 38 Tribonacci numbers once, store them in a static variable of the class Solution, and then just retrieve a needed number in a constant time during the testcase execution.

How to make the preliminary computations?

Two ideas could work here pretty well : recursion with memorisation and dynamic programming. They both need N operations to compute N Tribonacci numbers. Simple recursion like tribonacci(k) = tribonacci(k - 1) + tribonacci(k - 2) + tribonacci(k - 3) is out of consideration here because it will result in exponential time complexity .

Approach with preliminary computations has perfect O(1) runtime performance but needs in O(N) space to keep N Tribonacci numbers. In some quite rare cases it's crucial to optimise the space used above the performance.

In such a situation no more space-consuming static variables are allowed and one could use dynamic programming approach keeping not more than 3 Tribonacci numbers in memory.

fig


Approach 1: Space Optimisation : Dynamic Programming

  • If n < 3 the answer is obvious.

  • Otherwise initiate the first three numbers x = 0, y = z = 1 and proceed to the loop of n - 2 steps. At each step:

    • Replace x by y.

    • Replace y by z.

    • Replace z by x + y + z.

  • Return z.

Implementation

Complexity Analysis

  • Time complexity : .

  • Space complexity : constant space to keep the last three Fibonacci numbers.


Approach 2: Performance Optimisation : Recursion with Memorisation

  • Precompute 38 Tribonacci numbers:

    • Initiate array of precomputed Tribonacci numbers nums by zeros and initiate the first three numbers.

    • Return helper(n - 1).

  • Recursive function helper(k):

    • If k == 0, return 0.

    • If nums[k] != 0, return nums[k].

    • Otherwise, nums[k] = helper(k - 1) + helper(k - 2) + helper(k - 3). Return nums[k].

  • Retrieve needed Tribonacci number from the array of precomputed numbers.

Implementation

Complexity Analysis

  • Time complexity : to retrieve preliminary computed Tribonacci number, and 38 operations for the preliminary computations.

  • Space complexity : constant space to keep an array of 38 Tribonacci numbers.


Approach 3: Performance Optimisation : Dynamic Programming

  • Precompute 38 Tribonacci numbers:

    • Initiate an array of precomputed Tribonacci numbers nums by zeros and initiate the first three numbers.

    • Perform the loop for i in a range from 3 to 38. Compute at each step the new Tribonacci number: nums[i] = helper(i - 1) + helper(i - 2) + helper(i - 3).

  • Retrieve needed Tribonacci number from the array of precomputed numbers.

Implementation

Complexity Analysis

  • Time complexity : to retrieve preliminary computed Tribonacci number, and 38 operations for the preliminary computations.

  • Space complexity : constant space to keep an array of 38 Tribonacci numbers.

Analysis written by @liaison and @andvary

Source: rsshub.app

1137. N-th Tribonacci Number