diff --git a/leetcode/3601-3700/3623.Count-Number-of-Trapezoids-I/1.png b/leetcode/3601-3700/3623.Count-Number-of-Trapezoids-I/1.png new file mode 100644 index 00000000..44c9461d Binary files /dev/null and b/leetcode/3601-3700/3623.Count-Number-of-Trapezoids-I/1.png differ diff --git a/leetcode/3601-3700/3623.Count-Number-of-Trapezoids-I/2.png b/leetcode/3601-3700/3623.Count-Number-of-Trapezoids-I/2.png new file mode 100644 index 00000000..c19b048b Binary files /dev/null and b/leetcode/3601-3700/3623.Count-Number-of-Trapezoids-I/2.png differ diff --git a/leetcode/3601-3700/3623.Count-Number-of-Trapezoids-I/README.md b/leetcode/3601-3700/3623.Count-Number-of-Trapezoids-I/README.md index f0661db5..0a2f8037 100755 --- a/leetcode/3601-3700/3623.Count-Number-of-Trapezoids-I/README.md +++ b/leetcode/3601-3700/3623.Count-Number-of-Trapezoids-I/README.md @@ -1,28 +1,45 @@ # [3623.Count Number of Trapezoids I][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 +You are given a 2D integer array `points`, where `points[i] = [xi, yi]` represents the coordinates of the ith point on the Cartesian plane. + +A **horizontal trapezoid** is a convex quadrilateral with **at least one pair** of horizontal sides (i.e. parallel to the x-axis). Two lines are parallel if and only if they have the same slope. + +Return the number of unique **horizontal trapezoids** that can be formed by choosing any four distinct points from `points`. -**Example 1:** +Since the answer may be very large, return it **modulo** `10^9 + 7`. + +**Example 1:** + +![1](./1.png) ``` -Input: a = "11", b = "1" -Output: "100" +Input: points = [[1,0],[2,0],[3,0],[2,2],[3,2]] + +Output: 3 + +Explanation: + +There are three distinct ways to pick four points that form a horizontal trapezoid: + +Using points [1,0], [2,0], [3,2], and [2,2]. +Using points [2,0], [3,0], [3,2], and [2,2]. +Using points [1,0], [3,0], [3,2], and [2,2]. ``` -## 题意 -> ... +**Example 2:** -## 题解 +![2](./2.png) -### 思路1 -> ... -Count Number of Trapezoids I -```go ``` +Input: points = [[0,0],[1,0],[0,1],[2,1]] +Output: 1 + +Explanation: + +There is only one horizontal trapezoid that can be formed. +``` ## 结语 diff --git a/leetcode/3601-3700/3623.Count-Number-of-Trapezoids-I/Solution.go b/leetcode/3601-3700/3623.Count-Number-of-Trapezoids-I/Solution.go index d115ccf5..31d75056 100644 --- a/leetcode/3601-3700/3623.Count-Number-of-Trapezoids-I/Solution.go +++ b/leetcode/3601-3700/3623.Count-Number-of-Trapezoids-I/Solution.go @@ -1,5 +1,29 @@ package Solution -func Solution(x bool) bool { - return x +const mod = 1000000007 + +func Solution(points [][]int) int { + columns := make(map[int]int) + for _, p := range points { + columns[p[1]]++ + } + + pairs := []int{} + sum, count := 0, 0 + for _, c := range columns { + if c < 2 { + continue + } + count = c * (c - 1) / 2 + sum = (sum + count) % mod + pairs = append(pairs, count) + } + var ret int + for _, pair := range pairs { + ret = (ret + pair*((sum-pair+mod)%mod)) % mod + } + + // 使用模逆元乘以 1/2 + inv2 := (mod + 1) / 2 + return (ret * inv2) % mod } diff --git a/leetcode/3601-3700/3623.Count-Number-of-Trapezoids-I/Solution_test.go b/leetcode/3601-3700/3623.Count-Number-of-Trapezoids-I/Solution_test.go index 14ff50eb..de9e518b 100644 --- a/leetcode/3601-3700/3623.Count-Number-of-Trapezoids-I/Solution_test.go +++ b/leetcode/3601-3700/3623.Count-Number-of-Trapezoids-I/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{{1, 0}, {2, 0}, {3, 0}, {2, 2}, {3, 2}}, 3}, + {"TestCase2", [][]int{{0, 0}, {1, 0}, {0, 1}, {2, 1}}, 1}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }