Skip to content

Commit e2c2633

Browse files
poltaoyanglbme
andauthored
feat: add solutions to lc problem: No.1726 (#869)
No.1726.Tuple with Same Product Co-authored-by: Yang Libin <contact@yanglibin.info>
1 parent d080185 commit e2c2633

File tree

4 files changed

+92
-9
lines changed

4 files changed

+92
-9
lines changed

solution/1700-1799/1726.Tuple with Same Product/README.md

+35-4
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,53 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49+
**方法一:组合数+哈希表**
50+
51+
假设存在 `n` 组数,对于其中任意两组数 `a、b``c、d`,均满足 $a * b = c * d$ 的条件,则这样的组合一共有 $\mathrm{C}_n^2 = \frac{n*(n-1)}{2}$ 个。
52+
53+
根据题意每一组满足上述条件的组合可以构成 `8` 个满足题意的元组,故将各个相同乘积的组合数乘以 `8` 相加即可得到结果。
54+
55+
时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。
56+
4957
<!-- tabs:start -->
5058

5159
### **Python3**
5260

5361
<!-- 这里可写当前语言的特殊实现逻辑 -->
5462

5563
```python
56-
64+
class Solution:
65+
def tupleSameProduct(self, nums: List[int]) -> int:
66+
cnt = defaultdict(int)
67+
for i in range(1, len(nums)):
68+
for j in range(i):
69+
x = nums[i] * nums[j]
70+
cnt[x] += 1
71+
return sum(v * (v - 1) // 2 for v in cnt.values()) << 3
5772
```
5873

59-
### **Java**
74+
### **Go**
6075

6176
<!-- 这里可写当前语言的特殊实现逻辑 -->
6277

63-
```java
64-
78+
```go
79+
func tupleSameProduct(nums []int) int {
80+
product, n := make(map[int]int), len(nums)
81+
for i := 1; i < n; i++ {
82+
for j := 0; j < i; j++ {
83+
multiplier := nums[i] * nums[j]
84+
if _, ok := product[multiplier]; !ok {
85+
product[multiplier] = 0
86+
}
87+
product[multiplier] += 1
88+
}
89+
}
90+
ans := 0
91+
for _, v := range product {
92+
ans += v * (v - 1) / 2
93+
}
94+
return ans << 3
95+
}
6596
```
6697

6798
### **...**

solution/1700-1799/1726.Tuple with Same Product/README_EN.md

+32-5
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,45 @@
4040

4141
## Solutions
4242

43+
**Approach 1: Number of Combinations + Hash Table**
44+
45+
Time complexity $O(n^2)$, Space complexity $O(n^2)$.
46+
4347
<!-- tabs:start -->
4448

4549
### **Python3**
4650

4751
```python
48-
52+
class Solution:
53+
def tupleSameProduct(self, nums: List[int]) -> int:
54+
cnt = defaultdict(int)
55+
for i in range(1, len(nums)):
56+
for j in range(i):
57+
x = nums[i] * nums[j]
58+
cnt[x] += 1
59+
return sum(v * (v - 1) // 2 for v in cnt.values()) << 3
4960
```
5061

51-
### **Java**
52-
53-
```java
54-
62+
### **Go**
63+
64+
```go
65+
func tupleSameProduct(nums []int) int {
66+
product, n := make(map[int]int), len(nums)
67+
for i := 1; i < n; i++ {
68+
for j := 0; j < i; j++ {
69+
multiplier := nums[i] * nums[j]
70+
if _, ok := product[multiplier]; !ok {
71+
product[multiplier] = 0
72+
}
73+
product[multiplier] += 1
74+
}
75+
}
76+
ans := 0
77+
for _, v := range product {
78+
ans += v * (v - 1) / 2
79+
}
80+
return ans << 3
81+
}
5582
```
5683

5784
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func tupleSameProduct(nums []int) int {
2+
product, n := make(map[int]int), len(nums)
3+
for i := 1; i < n; i++ {
4+
for j := 0; j < i; j++ {
5+
multiplier := nums[i] * nums[j]
6+
if _, ok := product[multiplier]; !ok {
7+
product[multiplier] = 0
8+
}
9+
product[multiplier] += 1
10+
}
11+
}
12+
ans := 0
13+
for _, v := range product {
14+
ans += v * (v - 1) / 2
15+
}
16+
return ans << 3
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def tupleSameProduct(self, nums: List[int]) -> int:
3+
cnt = defaultdict(int)
4+
for i in range(1, len(nums)):
5+
for j in range(i):
6+
x = nums[i] * nums[j]
7+
cnt[x] += 1
8+
return sum(v * (v - 1) // 2 for v in cnt.values()) << 3

0 commit comments

Comments
 (0)