forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1726.java
42 lines (38 loc) · 1.33 KB
/
_1726.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
33
34
35
36
37
38
39
40
41
42
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.Map;
public class _1726 {
public static class Solution1 {
public int tupleSameProduct(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
int count = 0;
for (int i = 0; i < nums.length - 1; i++) {
for (int j = i + 1; j < nums.length; j++) {
int product = nums[i] * nums[j];
count += 8 * map.getOrDefault(product, 0);
map.put(product, map.getOrDefault(product, 0) + 1);
}
}
return count;
}
}
public static class Solution2 {
public int tupleSameProduct(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length - 1; i++) {
for (int j = i + 1; j < nums.length; j++) {
int product = nums[i] * nums[j];
map.put(product, map.getOrDefault(product, 0) + 1);
}
}
int count = 0;
for (int key : map.keySet()) {
if (map.get(key) > 1) {
int freq = map.get(key);
count += (freq * (freq - 1) / 2) * 8;
}
}
return count;
}
}
}