1236. N-th Tribonacci Number
My accepted C++ solution to LeetCode problem 1236, N-th Tribonacci Number, running in 0ms.
- Difficulty: Easy
- C++
- Runtime 0ms
- Memory 7.8MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
C++
Accepted on LeetCode — runtime 0ms, memory 7.8MB, accepted 2025-12-28.
class Solution {
public:
int tribonacci(int n) {
if (n == 0) return 0;
if (n <= 2) return 1;
int t0 = 0, t1 = 1, t2 = 1;
for (int i = 3; i <= n; i++) {
int t3 = t0 + t1 + t2;
t0 = t1;
t1 = t2;
t2 = t3;
}
return t2;
}
};