diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 0dae3bc..b7c376d 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -32,4 +32,5 @@ mod p2846_minimum_edge_weight_equilibrium_queries_in_a_tree; mod p2861_maximum_number_of_alloys; mod p365_water_and_jug_problem; mod p514_freedom_trail; -mod p2808_minimum_seconds_to_equalize_a_circular_array; \ No newline at end of file +mod p2808_minimum_seconds_to_equalize_a_circular_array; +mod p2670_find_the_distinct_difference_array; \ No newline at end of file diff --git a/src/problem/p2670_find_the_distinct_difference_array.rs b/src/problem/p2670_find_the_distinct_difference_array.rs new file mode 100644 index 0000000..7bc3611 --- /dev/null +++ b/src/problem/p2670_find_the_distinct_difference_array.rs @@ -0,0 +1,52 @@ +/** + * [2670] Find the Distinct Difference Array + */ +pub struct Solution {} + + +// submission codes start here + +use std::collections::HashMap; + +impl Solution { + pub fn distinct_difference_array(nums: Vec) -> Vec { + let mut right = HashMap::new(); + + for i in &nums { + let entry = right.entry(*i).or_insert(0); + *entry += 1; + } + + let mut result = Vec::with_capacity(nums.len()); + let mut left = HashMap::new(); + + for i in &nums { + let left_entry = left.entry(*i).or_insert(0); + *left_entry += 1; + + let right_entry = right.get_mut(i).unwrap(); + *right_entry -= 1; + + if *right_entry == 0 { + right.remove(i); + } + + result.push(left.len() as i32 - right.len() as i32); + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2670() { + assert_eq!(Solution::distinct_difference_array(vec![1, 2, 3, 4, 5]), + vec![-3, -1, 1, 3, 5]); + } +}