3162. Find the Number of Good Pairs I
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 86% Topics: Array, Hash Table
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(rlogr + n + m)
# Space: O(r)
import collections
# number theory, freq table
class Solution(object):
def numberOfPairs(self, nums1, nums2, k):
"""
:type nums1: List[int]
:type nums2: List[int]
:type k: int
:rtype: int
"""
cnt = [0]*(max(nums1)+1)
for x, c in collections.Counter(k*x for x in nums2).iteritems():
for i in xrange(1, (len(cnt)-1)//x+1):
cnt[i*x] += c
return sum(cnt[x] for x in nums1)
# Time: O(n * m)
# Space: O(1)
# brute force
class Solution2(object):
def numberOfPairs(self, nums1, nums2, k):
"""
:type nums1: List[int]
:type nums2: List[int]
:type k: int
:rtype: int
"""
return sum(x%(k*y) == 0 for x in nums1 for y in nums2)
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions