From 51c4dc623df95118bfbf14f3c548849adc70b467 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Mon, 19 Aug 2024 11:25:04 +0800 Subject: [PATCH] 20240819 Finished --- src/problem/mod.rs | 2 + .../p552_student_attendance_record_ii.rs | 61 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/problem/p552_student_attendance_record_ii.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 8900e6a..6e82130 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -209,3 +209,5 @@ mod p221_maximal_square; mod p3117_minimum_sum_of_values_by_dividing_array; mod p3137_minimum_number_of_operations_to_make_word_k_periodic; mod p551_student_attendance_record_i; + +mod p552_student_attendance_record_ii; \ No newline at end of file diff --git a/src/problem/p552_student_attendance_record_ii.rs b/src/problem/p552_student_attendance_record_ii.rs new file mode 100644 index 0000000..ce838cc --- /dev/null +++ b/src/problem/p552_student_attendance_record_ii.rs @@ -0,0 +1,61 @@ +/** + * [552] Student Attendance Record II + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn check_record(n: i32) -> i32 { + let mod_number = 1_000_000_007; + let n =n as usize; + + let mut dp = vec![vec![vec![0;3];2];n + 1]; + dp[0][0][0] = 1; + + for i in 1..=n { + // record ends with 'P' + for j in 0..2 { + for k in 0..3 { + dp[i][j][0] = (dp[i][j][0] + dp[i - 1][j][k]) % mod_number; + } + } + + // record ends with 'A' + for k in 0..3 { + dp[i][1][0] = (dp[i][1][0] + dp[i - 1][0][k]) % mod_number; + } + + // record ends with 'L' + for j in 0..2 { + for k in 1..3 { + dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j][k - 1]) % mod_number; + } + } + } + + let mut result = 0; + for i in 0..2 { + for j in 0..3 { + result = (result + dp[n][i][j]) % mod_number; + } + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_552() { + assert_eq!(8, Solution::check_record(2)); + assert_eq!(3, Solution::check_record(1)); + assert_eq!(183_236_316, Solution::check_record(10101)); + } +}