File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -51,6 +51,7 @@ mod a1012_numbers_with_repeated_digits;
5151mod a1018_binary_prefix_divisible_by_5;
5252mod a1021_remove_outermost_parentheses;
5353mod a1047_remove_all_adjacent_duplicates_in_string;
54+ mod a1137_n_th_tribonacci_number;
5455mod a1200_minimum_absolute_difference;
5556mod a1287_element_appearing_more_than_25_in_sorted_array;
5657mod a1302_deepest_leaves_sum;
You can’t perform that action at this time.
0 commit comments