Skip to content

Commit a4de42e

Browse files
author
liguanliang1
committed
feat: tribonacci
1 parent 8c8e4ca commit a4de42e

File tree

1 file changed

+22
-0
lines changed
  • leetcode/dynamic-programing

1 file changed

+22
-0
lines changed

leetcode/dynamic-programing/fib.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,25 @@ function fib(n) {
1919
return result
2020
}
2121

22+
// fib(n+3) = fib(n) + fib(n+1) + fib(n+2);
23+
24+
function tribonacci(n) {
25+
let fibn_1 = 0
26+
let fibn_2 = 1
27+
let fibn_3 = 1
28+
if (n === 0) { return fibn_1 }
29+
if (n === 1) { return fibn_2 }
30+
if (n === 2) { return fibn_3 }
31+
32+
let i = 3
33+
let result = fibn_1 + fibn_2 + fibn_3;
34+
while (i <= n) {
35+
result = fibn_1 + fibn_2 + fibn_3;
36+
fibn_1 = fibn_2;
37+
fibn_2 = fibn_3;
38+
fibn_3 = result;
39+
i++
40+
}
41+
42+
return result
43+
}

0 commit comments

Comments
 (0)