3164.优质数对的总数II
链接:3164.优质数对的总数II
难度:Medium
标签:数组、哈希表
简介:返回 优质数对 的总数。
题解 1 - python
- 编辑时间:2024-10-11
- 执行用时:1814ms
- 内存消耗:43.46MB
- 编程语言:python
- 解法介绍:计数存储后,遍历所有的因子
def factors(n):
return set(
factor for i in range(1, int(n ** 0.5) + 1) if n % i == 0
for factor in (i, n // i)
)
class Solution:
def numberOfPairs(self, nums1: List[int], nums2: List[int], k: int) -> int:
counter1 = Counter(nums1)
counter2 = Counter(nums2)
return sum(
sum(counter2[v] for v in factors(num // k)) * cnt
for num, cnt in counter1.items()
if num % k == 0
)