2024-01-07 20:00:24 +01:00
|
|
|
public class Solution {
|
|
|
|
public int Tribonacci(int n) {
|
2024-03-15 17:22:58 +01:00
|
|
|
var sequence = new int[] { 0, 1, 1 };
|
2024-01-07 20:00:24 +01:00
|
|
|
|
|
|
|
if (n < 3) {
|
|
|
|
return sequence[n];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 3; i <= n; ++i) {
|
|
|
|
var next = sequence.Sum();
|
|
|
|
sequence[0] = sequence[1];
|
|
|
|
sequence[1] = sequence[2];
|
|
|
|
sequence[2] = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sequence[2];
|
|
|
|
}
|
|
|
|
}
|