N-th Tribonacci Number
Compute the n-th Tribonacci number in O(n) time and O(1) space using three rolling variables.
01The problem
The Tribonacci sequence T is defined as follows:
T(0) = 0, T(1) = 1, T(2) = 1, and T(n) = T(n - 1) + T(n - 2) + T(n - 3) for n >= 3.
Given n, return the value of T(n).
02How to spot it
- The problem directly specifies a linear recurrence where each term is the sum of the preceding three terms.
- Plain recursion has exponential
O(3^n)time complexity due to redundant subproblem evaluations. - Because only the last three terms are required to compute the next, three rolling variables give an optimal
O(n)time andO(1)space solution.
03Pseudocode
if n == 0: return 0
if n <= 2: return 1
a, b, c = 0, 1, 1
repeat n - 2 times:
a, b, c = b, c, a + b + c
return c04Watch it work
sliding window [a, b, c]
computed
T(2) = 1
Initialize window with base terms: a = T(0) = 0, b = T(1) = 1, c = T(2) = 1.
Pseudocode
- if n == 0: return 0
- if n <= 2: return 1
- a, b, c = 0, 1, 1
- repeat n - 2 times:
- a, b, c = b, c, a + b + c
- return c
Variables
- a
- 0
- b
- 1
- c
- 1
05Step-by-step dry run
For n = 4:
- Base cases check:
n > 2. - Initialize
a = 0(T0),b = 1(T1),c = 1(T2). - Step 1 (
i = 3):next = a + b + c = 0 + 1 + 1 = 2.- Slide window:
a = 1,b = 1,c = 2(T3).
- Step 2 (
i = 4):next = a + b + c = 1 + 1 + 2 = 4.- Slide window:
a = 1,b = 2,c = 4(T4).
- Loop finishes (
n - 2 = 2iterations done). Returnc = 4.
06Java solution
public class Main {
public static int tribonacci(int n) {
if (n == 0) return 0;
if (n <= 2) return 1;
int a = 0, b = 1, c = 1;
for (int i = 3; i <= n; i++) {
int next = a + b + c;
a = b;
b = c;
c = next;
}
return c;
}
public static void main(String[] args) {
System.out.println(tribonacci(4));
System.out.println(tribonacci(25));
}
}4 1389537
07Why it works
Each Tribonacci number depends strictly on the three preceding values. By maintaining variables a, b, and c for T(i-3), T(i-2), and T(i-1), computing next = a + b + c evaluates T(i) in O(1) time. Shifting variables forward prepares the state for step i + 1.
08Time and space
- Time complexity:
O(n)because the loop executesn - 2times. - Space complexity:
O(1)using three scalar integer registers.
09Edge cases
n = 0: Handled immediately by base check, returns0.n = 1orn = 2: Handled immediately, returns1.n = 37: Max constraint; result fits comfortably in a standard 32-bit signed integer.
10Common mistakes
- Using naive recursion:
tribonacci(n - 1) + tribonacci(n - 2) + tribonacci(n - 3)without memoization triggers time-limit exceeded (O(3^n)). - Allocating an array of size n: While
O(n)space passes, using an array is unnecessary when only the previous 3 terms matter.
11Other approaches
- Matrix Exponentiation: Can compute the n-th term in
O(log n)time using[[1,1,1],[1,0,0],[0,1,0]]^(n-2), useful whennis up to10^9.
12In the interview
- Point out that this is Fibonacci extended to 3 terms.
- Emphasize space optimization: "I will use 3 variables instead of an array of size n to achieve O(1) extra memory."
13Quiz
What is the space complexity of computing the n-th Tribonacci number using rolling variables?
- (Recommended) O(1)
- O(n)
- O(log n)
- O(3^n)
Answer: O(1). Only three scalar variables are updated on each step regardless of n.
14Solve it yourself
Open LeetCode #1137 and submit the rolling variable solution.