Skip to content

Commit a06160a

Browse files
authored
Create number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers.py
1 parent 70db21f commit a06160a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Time: O(m * n)
2+
# Space: O(m + n)
3+
4+
import collections
5+
6+
7+
class Solution(object):
8+
def numTriplets(self, nums1, nums2):
9+
"""
10+
:type nums1: List[int]
11+
:type nums2: List[int]
12+
:rtype: int
13+
"""
14+
def two_product(nums, i):
15+
count = 0
16+
lookup = collections.defaultdict(int)
17+
for num in nums:
18+
if i%num:
19+
continue
20+
count += lookup[i//num]
21+
lookup[num] += 1
22+
return count
23+
24+
result = 0
25+
for num in nums1:
26+
result += two_product(nums2, num**2)
27+
for num in nums2:
28+
result += two_product(nums1, num**2)
29+
return result

0 commit comments

Comments
 (0)