Skip to content

Commit 798585e

Browse files
authored
Merge pull request #1069 from 0xff-dev/1726
Add solution and test-cases for problem 1726
2 parents e7f129f + b5671db commit 798585e

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

leetcode/1701-1800/1726.Tuple-with-Same-Product/README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
# [1726.Tuple with Same Product][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given an array `nums` of **distinct** positive integers, return the number of tuples `(a, b, c, d)` such that `a * b = c * d` where `a`, `b`, `c`, and `d` are elements of `nums`, and `a != b != c != d`.
75

86
**Example 1:**
97

108
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
9+
Input: nums = [2,3,4,6]
10+
Output: 8
11+
Explanation: There are 8 valid tuples:
12+
(2,6,3,4) , (2,6,4,3) , (6,2,3,4) , (6,2,4,3)
13+
(3,4,2,6) , (4,3,2,6) , (3,4,6,2) , (4,3,6,2)
1314
```
1415

15-
## 题意
16-
> ...
17-
18-
## 题解
16+
**Example 2:**
1917

20-
### 思路1
21-
> ...
22-
Tuple with Same Product
23-
```go
2418
```
25-
19+
Input: nums = [1,2,4,5,10]
20+
Output: 16
21+
Explanation: There are 16 valid tuples:
22+
(1,10,2,5) , (1,10,5,2) , (10,1,2,5) , (10,1,5,2)
23+
(2,5,1,10) , (2,5,10,1) , (5,2,1,10) , (5,2,10,1)
24+
(2,10,4,5) , (2,10,5,4) , (10,2,4,5) , (10,2,5,4)
25+
(4,5,2,10) , (4,5,10,2) , (5,4,2,10) , (5,4,10,2)
26+
```
2627

2728
## 结语
2829

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
m := make(map[int]int)
5+
l := len(nums)
6+
for i := 0; i < l-1; i++ {
7+
for j := i + 1; j < l; j++ {
8+
cur := nums[i] * nums[j]
9+
m[cur]++
10+
}
11+
}
12+
ans := 0
13+
for _, n := range m {
14+
ans += (n - 1) * n / 2 * 8
15+
}
16+
return ans
517
}

leetcode/1701-1800/1726.Tuple-with-Same-Product/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{2, 3, 4, 6}, 8},
17+
{"TestCase2", []int{1, 2, 4, 5, 10}, 16},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)