From 9238af57e834011dd2a8741be2530cf5dd177f74 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Thu, 28 Nov 2024 12:01:08 +0800 Subject: [PATCH] 20241128 finished. --- src/problem/mod.rs | 2 + ...250_find_the_count_of_monotonic_pairs_i.rs | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/problem/p3250_find_the_count_of_monotonic_pairs_i.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 4540b78..ff935a0 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -340,3 +340,5 @@ mod p632_smallest_range_covering_elements_from_k_lists; mod p3206_alternating_groups_i; mod p3208_alternating_groups_ii; + +mod p3250_find_the_count_of_monotonic_pairs_i; diff --git a/src/problem/p3250_find_the_count_of_monotonic_pairs_i.rs b/src/problem/p3250_find_the_count_of_monotonic_pairs_i.rs new file mode 100644 index 0000000..b55225b --- /dev/null +++ b/src/problem/p3250_find_the_count_of_monotonic_pairs_i.rs @@ -0,0 +1,47 @@ +/** + * [3250] Find the Count of Monotonic Pairs I + */ +pub struct Solution {} + +// submission codes start here + +const MOD: i32 = 1_000_000_007; + +impl Solution { + pub fn count_of_pairs(nums: Vec) -> i32 { + let n = nums.len(); + let nums: Vec = nums.into_iter().map(|x| x as usize).collect(); + let max_num = *nums.iter().max().unwrap(); + + let mut dp = vec![vec![0; max_num + 1]; n]; + + for i in 0..=nums[0] { + dp[0][i] = 1; + } + + for i in 1..n { + for v2 in 0..=nums[i] { + for v1 in 0..=v2 { + if nums[i - 1] >= v1 && nums[i - 1] - v1 >= nums[i] - v2 { + dp[i][v2] = (dp[i][v2] + dp[i - 1][v1]) % MOD; + } + } + } + } + + dp[n - 1].iter().fold(0, |acc, e| (acc + *e) % MOD) + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3250() { + assert_eq!(4, Solution::count_of_pairs(vec![2, 3, 2])); + assert_eq!(126, Solution::count_of_pairs(vec![5, 5, 5, 5])); + } +}