Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d053c60

Browse files
author
guangsheng.li01
committed
Solved 1137
1 parent 75c124f commit d053c60

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/a1137_n_th_tribonacci_number.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* [1137] n-th-tribonacci-number
3+
*/
4+
5+
struct Solution;
6+
7+
impl Solution {
8+
pub fn tribonacci(n: i32) -> i32 {
9+
let mut dp: [i32; 38] = [0; 38];
10+
dp[0] = 0;
11+
dp[1] = 1;
12+
dp[2] = 1;
13+
for i in 3..(n + 1) as usize {
14+
dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3];
15+
}
16+
dp[n as usize]
17+
}
18+
}
19+
20+
#[cfg(test)]
21+
mod tests {
22+
use super::*;
23+
24+
#[test]
25+
fn test_case0() {
26+
assert_eq!(Solution::tribonacci(4), 4);
27+
assert_eq!(Solution::tribonacci(25), 1389537);
28+
assert_eq!(Solution::tribonacci(30), 29249425);
29+
}
30+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ mod a1012_numbers_with_repeated_digits;
5151
mod a1018_binary_prefix_divisible_by_5;
5252
mod a1021_remove_outermost_parentheses;
5353
mod a1047_remove_all_adjacent_duplicates_in_string;
54+
mod a1137_n_th_tribonacci_number;
5455
mod a1200_minimum_absolute_difference;
5556
mod a1287_element_appearing_more_than_25_in_sorted_array;
5657
mod a1302_deepest_leaves_sum;

0 commit comments

Comments
 (0)