Skip to content

Commit a352352

Browse files
refactor 1726
1 parent 3b20425 commit a352352

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/main/java/com/fishercoder/solutions/_1726.java

+20
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@ public int tupleSameProduct(int[] nums) {
1717
}
1818
return count;
1919
}
20+
}
21+
22+
public static class Solution2 {
23+
public int tupleSameProduct(int[] nums) {
24+
Map<Integer, Integer> map = new HashMap<>();
25+
for (int i = 0; i < nums.length - 1; i++) {
26+
for (int j = i + 1; j < nums.length; j++) {
27+
int product = nums[i] * nums[j];
28+
map.put(product, map.getOrDefault(product, 0) + 1);
29+
}
30+
}
31+
int count = 0;
32+
for (int key : map.keySet()) {
33+
if (map.get(key) > 1) {
34+
int freq = map.get(key);
35+
count += (freq * (freq - 1) / 2) * 8;
36+
}
37+
}
38+
return count;
39+
}
2040

2141
}
2242
}

src/test/java/com/fishercoder/_1726Test.java

+6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88

99
public class _1726Test {
1010
private static _1726.Solution1 solution1;
11+
private static _1726.Solution2 solution2;
1112

1213
@BeforeClass
1314
public static void setup() {
1415
solution1 = new _1726.Solution1();
16+
solution2 = new _1726.Solution2();
1517
}
1618

1719
@Test
@@ -39,4 +41,8 @@ public void test5() {
3941
assertEquals(128, solution1.tupleSameProduct(new int[]{1, 2, 3, 4, 6, 8, 12, 24}));
4042
}
4143

44+
@Test
45+
public void test6() {
46+
assertEquals(40, solution2.tupleSameProduct(new int[]{2, 3, 4, 6, 8, 12}));
47+
}
4248
}

0 commit comments

Comments
 (0)