Skip to content

Commit dbbfd54

Browse files
committed
Add problem 3162: Find the Number of Good Pairs I
1 parent e59aa8f commit dbbfd54

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,6 +2114,7 @@ pub mod problem_3153_sum_of_digit_differences_of_all_pairs;
21142114
pub mod problem_3158_find_the_xor_of_numbers_which_appear_twice;
21152115
pub mod problem_3159_find_occurrences_of_an_element_in_an_array;
21162116
pub mod problem_3160_find_the_number_of_distinct_colors_among_the_balls;
2117+
pub mod problem_3162_find_the_number_of_good_pairs_i;
21172118
pub mod problem_3164_find_the_number_of_good_pairs_ii;
21182119
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
21192120

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
use std::collections::HashMap;
6+
use std::collections::hash_map::Entry;
7+
8+
impl Solution {
9+
pub fn number_of_pairs(nums1: Vec<i32>, nums2: Vec<i32>, k: i32) -> i64 {
10+
let nums1 = nums1.into_iter().map(i32::cast_unsigned).collect::<Vec<_>>();
11+
let nums2 = nums2.into_iter().map(i32::cast_unsigned).collect::<Vec<_>>();
12+
let k = k.cast_unsigned();
13+
let max = nums1.iter().copied().max().unwrap_or(0);
14+
let mut counts_1 = vec![0_u32; max as usize + 1].into_boxed_slice();
15+
16+
for num in nums1 {
17+
counts_1[num as usize] += 1;
18+
}
19+
20+
let mut counts_2 = HashMap::new();
21+
22+
for num in nums2 {
23+
match counts_2.entry(num * k) {
24+
Entry::Occupied(occupied_entry) => *occupied_entry.into_mut() += 1,
25+
Entry::Vacant(vacant_entry) => {
26+
vacant_entry.insert(1_u32);
27+
}
28+
}
29+
}
30+
31+
counts_2
32+
.into_iter()
33+
.filter_map(|(factor, count)| {
34+
let factor = factor as usize;
35+
36+
counts_1
37+
.get(factor..)
38+
.map(|range| range.iter().step_by(factor).copied().map(u64::from).sum::<u64>() * u64::from(count))
39+
})
40+
.sum::<u64>()
41+
.cast_signed()
42+
}
43+
}
44+
45+
// ------------------------------------------------------ snip ------------------------------------------------------ //
46+
47+
impl super::Solution for Solution {
48+
fn number_of_pairs(nums1: Vec<i32>, nums2: Vec<i32>, k: i32) -> i64 {
49+
Self::number_of_pairs(nums1, nums2, k)
50+
}
51+
}
52+
53+
#[cfg(test)]
54+
mod tests {
55+
#[test]
56+
fn test_solution() {
57+
super::super::tests::run::<super::Solution>();
58+
}
59+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
pub mod hash_map;
2+
3+
pub trait Solution {
4+
fn number_of_pairs(nums1: Vec<i32>, nums2: Vec<i32>, k: i32) -> i64;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [
13+
((&[1, 3, 4] as &[_], &[1, 3, 4] as &[_], 1), 5),
14+
((&[1, 2, 4, 12], &[2, 4], 3), 2),
15+
((&[1], &[8], 1), 0),
16+
];
17+
18+
for ((nums1, nums2, k), expected) in test_cases {
19+
assert_eq!(S::number_of_pairs(nums1.to_vec(), nums2.to_vec(), k), expected);
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)