1-D Dynamic Programming Take it or skip it
520. N-th Tribonacci Number
Easy
The Tribonacci sequence starts with the three terms 0, 1 and 1. Every later term is the sum of the three terms just before it: 0, 1, 1, 2, 4, 7, 13, 24, and so on.
T(n) is the term at index n, counting from 0:
- T(0) = 0
- T(1) = 1
- T(2) = 1
- T(3) = T(2) + T(1) + T(0) = 1 + 1 + 0 = 2
- T(4) = T(3) + T(2) + T(1) = 2 + 1 + 1 = 4
Given an integer n, return T(n).
Example 1
Input: n = 7
Output:
24Explanation: The terms at indexes 0 to 7 are 0, 1, 1, 2, 4, 7, 13 and 24. The last one, 24, is 4 + 7 + 13.
Example 2
Input: n = 0
Output:
0Explanation: The sequence starts at index 0, and its first term is 0.
Example 3
Input: n = 37
Output:
2082876103Check your understanding
Work out the answer for this input before you write any code.
n = 8
Constraints
0 ≤ n ≤ 37
Clarifying questions
What you would ask an interviewer before writing any code.