The Indian Engineer

Problem 1137 Nth Tribonacci Number

Posted on 2 mins

Cpp Math Dp

Problem Statement

Link - Problem 1137

Question

The Tribonacci sequence T(n) is defined as follows:

T(0) = 0, T(1) = 1, T(2) = 1, and T(n+3) = T(n) + T(n+1) + T(n+2) for n >= 0.

Given n, return the value of T(n).

Note to self

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

Solution

class Solution {
public:
    int tribonacci(int n) {
        if(!n)
            return 0;
        if(n==1)
            return 1;
        if(n==2)
            return 1;
        vector<int>dp(n+1);
        dp[0]= 0;
        dp[1] = 1;
        dp[2] = 1;
        for(int i=3;i<=n;i++)
            dp[i]= dp[i-1]+dp[i-2]+dp[i-3];
        return dp[n];
    }
};

Complexity

Explaination

  1. We will use the DP array to store the intermediate values.
  2. We will store 0,1,1 as first 3 values.
  3. Then inside a loop from 3 to n, we will calculate the value of T(n) using the formula T(n) = T(n-1) + T(n-2) + T(n-3).
  4. We will return the value of T(n) at the end.

DP without using array

    class Solution {
    public:
        int tribonacci(int n) {
            if(!n)
                return 0;
            if(n==1)
                return 1;
            if(n==2)
                return 1;
            int a=0,b=1,c=1;
            for(int i=3;i<=n;i++)
            {
                int temp = a+b+c;
                a=b;
                b=c;
                c=temp;
            }
            return c;
        }
    };

This is a demonstration of memoization technique used in DP. With this we can convert a recursive method into dynamic programming method.