20240131 Finished

This commit is contained in:
jackfiled 2024-01-31 10:26:18 +08:00
parent 916b49fc26
commit 06d2f3a149
2 changed files with 54 additions and 1 deletions

View File

@ -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;
mod p2808_minimum_seconds_to_equalize_a_circular_array;
mod p2670_find_the_distinct_difference_array;

View File

@ -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<i32>) -> Vec<i32> {
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]);
}
}