mirror of
https://github.com/TheAlgorithms/C.git
synced 2026-04-23 22:09:37 +00:00
* add leetcode n-th Tribonacci number * updating DIRECTORY.md * updating DIRECTORY.md --------- Co-authored-by: github-actions[bot] <github-actions@users.noreply.github.com>
30 lines
435 B
C
30 lines
435 B
C
// Dynamic Programming
|
|
// Runtime: O(n)
|
|
// Space: O(1)
|
|
int tribonacci(int n){
|
|
int t0 = 0;
|
|
int t1 = 1;
|
|
int t2 = 1;
|
|
|
|
if (n == 0) {
|
|
return t0;
|
|
}
|
|
|
|
if (n == 1){
|
|
return t1;
|
|
}
|
|
|
|
if (n == 2){
|
|
return t2;
|
|
}
|
|
|
|
for (int i = 0; i < n - 2; i++){
|
|
int nextT = t0 + t1 + t2;
|
|
t0 = t1;
|
|
t1 = t2;
|
|
t2 = nextT;
|
|
}
|
|
|
|
return t2;
|
|
}
|