Math Fundamentals18 sections · 814 units
Open in Course

Practice - Tribonacci Number

LeetCode 1137

The Tribonacci sequence is defined as T0=0T_0 = 0, T1=1T_1 = 1, T2=1T_2 = 1, and Tn=Tn1+Tn2+Tn3T_n = T_{n-1} + T_{n-2} + T_{n-3} for n3n \geq 3.

Given nn, compute TnT_n. This is like Fibonacci but with three terms instead of two.

Hint: use dynamic programming with O(n)O(n) time and O(1)O(1) space by tracking only the last three values.