Files
C/leetcode/src/1137.c
Alexander Pantyukhin 9fa578d4b5 feat: add N-th Tribonacci number (#1202)
* add leetcode n-th Tribonacci number

* updating DIRECTORY.md

* updating DIRECTORY.md

---------

Co-authored-by: github-actions[bot] <github-actions@users.noreply.github.com>
2023-02-02 19:58:32 -06:00

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;
}