From b5671db65a9e0be80ecaeeb633489aaa33e17c16 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Fri, 20 Dec 2024 09:10:52 +0800 Subject: [PATCH] Add solution and test-cases for problem 1726 --- .../1726.Tuple-with-Same-Product/README.md | 29 ++++++++++--------- .../1726.Tuple-with-Same-Product/Solution.go | 16 ++++++++-- .../Solution_test.go | 13 ++++----- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/leetcode/1701-1800/1726.Tuple-with-Same-Product/README.md b/leetcode/1701-1800/1726.Tuple-with-Same-Product/README.md index 3d4fcee94..cf362fa9d 100755 --- a/leetcode/1701-1800/1726.Tuple-with-Same-Product/README.md +++ b/leetcode/1701-1800/1726.Tuple-with-Same-Product/README.md @@ -1,28 +1,29 @@ # [1726.Tuple with Same Product][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [2,3,4,6] +Output: 8 +Explanation: There are 8 valid tuples: +(2,6,3,4) , (2,6,4,3) , (6,2,3,4) , (6,2,4,3) +(3,4,2,6) , (4,3,2,6) , (3,4,6,2) , (4,3,6,2) ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Tuple with Same Product -```go ``` - +Input: nums = [1,2,4,5,10] +Output: 16 +Explanation: There are 16 valid tuples: +(1,10,2,5) , (1,10,5,2) , (10,1,2,5) , (10,1,5,2) +(2,5,1,10) , (2,5,10,1) , (5,2,1,10) , (5,2,10,1) +(2,10,4,5) , (2,10,5,4) , (10,2,4,5) , (10,2,5,4) +(4,5,2,10) , (4,5,10,2) , (5,4,2,10) , (5,4,10,2) +``` ## 结语 diff --git a/leetcode/1701-1800/1726.Tuple-with-Same-Product/Solution.go b/leetcode/1701-1800/1726.Tuple-with-Same-Product/Solution.go index d115ccf5e..28b71941d 100644 --- a/leetcode/1701-1800/1726.Tuple-with-Same-Product/Solution.go +++ b/leetcode/1701-1800/1726.Tuple-with-Same-Product/Solution.go @@ -1,5 +1,17 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) int { + m := make(map[int]int) + l := len(nums) + for i := 0; i < l-1; i++ { + for j := i + 1; j < l; j++ { + cur := nums[i] * nums[j] + m[cur]++ + } + } + ans := 0 + for _, n := range m { + ans += (n - 1) * n / 2 * 8 + } + return ans } diff --git a/leetcode/1701-1800/1726.Tuple-with-Same-Product/Solution_test.go b/leetcode/1701-1800/1726.Tuple-with-Same-Product/Solution_test.go index 14ff50eb4..82aa4be65 100644 --- a/leetcode/1701-1800/1726.Tuple-with-Same-Product/Solution_test.go +++ b/leetcode/1701-1800/1726.Tuple-with-Same-Product/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{2, 3, 4, 6}, 8}, + {"TestCase2", []int{1, 2, 4, 5, 10}, 16}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }