Problem Description
The Tribonacci sequence Tn is defined as follows:
and for .
Given , return the value of .
Examples
Example 1
Input: n = 4
Output: 4
Explanation: = 0 + 1 + 1 = 2 = 1 + 1 + 2 = 4
Example 2
Input: n = 25
Output: 1389537
Thought Process / Intuition
Bottom up dynamic programming, returning the last item in the dp array.
Approach
- Create a dp array of length 
n + 1, where the value at the index of the array is the value of . - Initialize the zeroth, first and second values of the dp array to 0, 1 and 1 respectively, since we are given
 - Iterate from the 
3ton, calculating and storing the in the dp array - Return the last item in the dp array.
 
Solution
class Solution:
    def tribonacci(self, n: int) -> int:
        if n == 0:
            return 0
        if n == 1:
            return 1
        if n == 2:
            return 1
        dp = [0] * (n + 1)
        dp[0] = 0
        dp[1] = 1
        dp[2] = 1
        for i in range(3, n + 1):
            dp[i] = dp[i-1] + dp[i-2] + dp[i-3]
        return dp[n] 
Complexity Analysis
Time complexity:
For loop is length .
- Space complexity:
 
Space complexity:
Size of the DP array is .