From 1cfe77f7779454c6519312657c09aaf90d1a9ad5 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Tue, 22 Oct 2024 14:29:41 +0800 Subject: [PATCH] 20241022 finished. --- src/problem/mod.rs | 3 +- ..._count_pairs_that_form_a_complete_day_i.rs | 40 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/problem/p3184_count_pairs_that_form_a_complete_day_i.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index f9d420d..bde2ad9 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -270,4 +270,5 @@ mod p3193_count_the_number_of_inversions; mod p3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i; mod p3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii; mod p908_smallest_range_i; -mod p910_smallest_range_ii; \ No newline at end of file +mod p910_smallest_range_ii; +mod p3184_count_pairs_that_form_a_complete_day_i; \ No newline at end of file diff --git a/src/problem/p3184_count_pairs_that_form_a_complete_day_i.rs b/src/problem/p3184_count_pairs_that_form_a_complete_day_i.rs new file mode 100644 index 0000000..7fd4e67 --- /dev/null +++ b/src/problem/p3184_count_pairs_that_form_a_complete_day_i.rs @@ -0,0 +1,40 @@ +/** + * [3184] Count Pairs That Form a Complete Day I + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn count_complete_day_pairs(hours: Vec) -> i32 { + use std::collections::HashMap; + + let mut map = HashMap::new(); + let mut result = 0; + + for hour in hours { + let hour = hour % 24; + if let Some(&c) = map.get(&((24 - hour) % 24)) { + result += c; + } + let entry = map.entry(hour).or_insert(0); + *entry += 1; + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3184() { + assert_eq!(3, Solution::count_complete_day_pairs(vec![72, 48, 24, 3])); + assert_eq!(2, Solution::count_complete_day_pairs(vec![12, 12, 30, 24, 24])); + } +}