- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathTribonacci.java
38 lines (33 loc) · 929 Bytes
/
Tribonacci.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
packagecom.thealgorithms.dynamicprogramming;
/**
* The {@code Tribonacci} class provides a method to compute the n-th number in the Tribonacci sequence.
* N-th Tribonacci Number - https://leetcode.com/problems/n-th-tribonacci-number/description/
*/
publicfinalclassTribonacci {
privateTribonacci() {
}
/**
* Computes the n-th Tribonacci number.
*
* @param n the index of the Tribonacci number to compute
* @return the n-th Tribonacci number
*/
publicstaticintcompute(intn) {
if (n == 0) {
return0;
}
if (n == 1 || n == 2) {
return1;
}
intfirst = 0;
intsecond = 1;
intthird = 1;
for (inti = 3; i <= n; i++) {
intnext = first + second + third;
first = second;
second = third;
third = next;
}
returnthird;
}
}