cs: add “1137. N-th Tribonacci Number”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
b6f5b99ee1
commit
4cca12685d
1 changed files with 18 additions and 0 deletions
18
cs/n-th-tribonacci-number.cs
Normal file
18
cs/n-th-tribonacci-number.cs
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
public class Solution {
|
||||||
|
public int Tribonacci(int n) {
|
||||||
|
var sequence = new int[]{0, 1, 1};
|
||||||
|
|
||||||
|
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];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue