20241011 finished.

This commit is contained in:
jackfiled 2024-10-11 14:58:56 +08:00
parent bb6c1b09fe
commit 4533063186
2 changed files with 61 additions and 1 deletions

View File

@ -259,4 +259,5 @@ mod p2187_minimum_time_to_complete_trips;
mod p871_minimum_number_of_refueling_stops; mod p871_minimum_number_of_refueling_stops;
mod p1436_destination_city; mod p1436_destination_city;
mod p3171_find_subarray_with_bitwise_or_closest_to_k; mod p3171_find_subarray_with_bitwise_or_closest_to_k;
mod p3162_find_the_number_of_good_pairs_i; mod p3162_find_the_number_of_good_pairs_i;
mod p3164_find_the_number_of_good_pairs_ii;

View File

@ -0,0 +1,59 @@
/**
* [3164] Find the Number of Good Pairs II
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn number_of_pairs(nums1: Vec<i32>, nums2: Vec<i32>, k: i32) -> i64 {
use std::collections::HashMap;
let mut nums1_count = HashMap::with_capacity(nums1.len());
let mut nums1_max = 0;
for i in nums1 {
let entry = nums1_count.entry(i).or_insert(0);
*entry += 1;
nums1_max = nums1_max.max(i);
}
let mut nums2_count = HashMap::with_capacity(nums2.len());
for i in nums2 {
let entry = nums2_count.entry(i).or_insert(0);
*entry += 1;
}
let mut result = 0;
for (i, count) in nums2_count {
let mut value = i * k;
while value <= nums1_max {
if let Some(&count1) = nums1_count.get(&value) {
result += count1 * count;
}
value += i * k;
}
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3164() {
assert_eq!(5, Solution::number_of_pairs(vec![1, 3, 4], vec![1, 3, 4], 1));
assert_eq!(2, Solution::number_of_pairs(vec![1, 2, 4, 12], vec![2, 4], 3));
}
}