You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/1700-1799/1726.Tuple with Same Product/README_EN.md
+52-3
Original file line number
Diff line number
Diff line change
@@ -40,9 +40,13 @@
40
40
41
41
## Solutions
42
42
43
-
**Solution 1: Number of Combinations + Hash Table**
43
+
**Solution 1: Combination + Hash Table**
44
44
45
-
Time complexity $O(n^2)$, Space complexity $O(n^2)$.
45
+
Assuming there are $n$ pairs of numbers, for any two pairs of numbers $a, b$ and $c, d$ that satisfy the condition $a \times b = c \times d$, there are a total of $\mathrm{C}_n^2 = \frac{n \times (n-1)}{2}$ such combinations.
46
+
47
+
According to the problem description, each combination that satisfies the above condition can form $8$ tuples that satisfy the problem requirements. Therefore, we can multiply the number of combinations with the same product by $8$ (equivalent to left shifting by $3$ bits) and add them up to get the result.
48
+
49
+
The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the length of the array.
46
50
47
51
<!-- tabs:start -->
48
52
@@ -68,7 +72,7 @@ class Solution {
68
72
for (int i =1; i < nums.length; ++i) {
69
73
for (int j =0; j < i; ++j) {
70
74
int x = nums[i] * nums[j];
71
-
cnt.put(x, cnt.getOrDefault(x, 0) +1);
75
+
cnt.merge(x, 1, Integer::sum);
72
76
}
73
77
}
74
78
int ans =0;
@@ -121,6 +125,51 @@ func tupleSameProduct(nums []int) int {
0 commit comments