1. Description
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$.
2. Example
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
3. Constraints
- 0 <= n <= 37
- The answer is guaranteed to fit within a 32-bit integer, ie. answer <= 2$^{31}$ - 1.
4. Solutions
Dynamic Programming
Time complexity: O(n)
Space complexity: O(1)
| |