forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_1577.java
32 lines (28 loc) · 909 Bytes
/
_1577.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.Map;
public class _1577 {
public static class Solution1 {
public int numTriplets(int[] nums1, int[] nums2) {
long result = 0;
for (long num : nums1) {
result += twoProduct(num * num, nums2);
}
for (long num : nums2) {
result += twoProduct(num * num, nums1);
}
return (int) result;
}
private long twoProduct(long product, int[] nums) {
Map<Long, Long> map = new HashMap<>();
long count = 0;
for (long num : nums) {
if (product % num == 0) {
count += map.getOrDefault(product / num, 0L);
}
map.put(num, map.getOrDefault(num, 0L) + 1);
}
return count;
}
}
}