From 443c8fff12d5c8100be4200ed01cc43f4f450abb Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sun, 27 Apr 2025 11:38:16 +0800 Subject: [PATCH] 20250427 finished. --- src/problem/mod.rs | 2 ++ ...arrays_of_length_three_with_a_condition.rs | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/problem/p3392_count_subarrays_of_length_three_with_a_condition.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index ca353ca..8c33fcf 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -621,3 +621,5 @@ mod p2845_count_of_interesting_subarrays; mod p1287_element_appearing_more_than_25_in_sorted_array; mod p2444_count_subarrays_with_fixed_bounds; + +mod p3392_count_subarrays_of_length_three_with_a_condition; diff --git a/src/problem/p3392_count_subarrays_of_length_three_with_a_condition.rs b/src/problem/p3392_count_subarrays_of_length_three_with_a_condition.rs new file mode 100644 index 0000000..50da755 --- /dev/null +++ b/src/problem/p3392_count_subarrays_of_length_three_with_a_condition.rs @@ -0,0 +1,35 @@ +/** + * [3392] Count Subarrays of Length Three With a Condition + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn count_subarrays(nums: Vec) -> i32 { + nums.iter() + .enumerate() + .skip(2) + .filter_map(|(i, &v)| { + if (nums[i - 2] + v) * 2 == nums[i - 1] { + Some(()) + } else { + None + } + }) + .count() as i32 + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3392() { + assert_eq!(1, Solution::count_subarrays(vec![1, 2, 1, 4, 1])); + assert_eq!(0, Solution::count_subarrays(vec![1, 1, 1])); + } +}