rs: add «1137. N-th Tribonacci Number»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
14f67a9637
commit
c45234e58f
1 changed files with 17 additions and 0 deletions
17
rs/n-th-tribonacci-number.rs
Normal file
17
rs/n-th-tribonacci-number.rs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
impl Solution {
|
||||||
|
pub fn tribonacci(mut n: i32) -> i32 {
|
||||||
|
let mut seq = vec![0, 1, 1];
|
||||||
|
|
||||||
|
while n >= 3 {
|
||||||
|
let next = seq.iter().sum::<i32>();
|
||||||
|
|
||||||
|
seq[0] = seq[1];
|
||||||
|
seq[1] = seq[2];
|
||||||
|
seq[2] = next;
|
||||||
|
|
||||||
|
n -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
seq[n as usize]
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue