File tree 4 files changed +92
-9
lines changed
solution/1700-1799/1726.Tuple with Same Product
4 files changed +92
-9
lines changed Original file line number Diff line number Diff line change 46
46
47
47
<!-- 这里可写通用的实现逻辑 -->
48
48
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
+
49
57
<!-- tabs:start -->
50
58
51
59
### ** Python3**
52
60
53
61
<!-- 这里可写当前语言的特殊实现逻辑 -->
54
62
55
63
``` 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
57
72
```
58
73
59
- ### ** Java **
74
+ ### ** Go **
60
75
61
76
<!-- 这里可写当前语言的特殊实现逻辑 -->
62
77
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
+ }
65
96
```
66
97
67
98
### ** ...**
Original file line number Diff line number Diff line change 40
40
41
41
## Solutions
42
42
43
+ ** Approach 1: Number of Combinations + Hash Table**
44
+
45
+ Time complexity $O(n^2)$, Space complexity $O(n^2)$.
46
+
43
47
<!-- tabs:start -->
44
48
45
49
### ** Python3**
46
50
47
51
``` 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
49
60
```
50
61
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
+ }
55
82
```
56
83
57
84
### ** ...**
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments