diff --git a/leetcode/3601-3700/3625.Count-Number-of-Trapezoids-II/1.png b/leetcode/3601-3700/3625.Count-Number-of-Trapezoids-II/1.png new file mode 100644 index 00000000..a0cb8492 Binary files /dev/null and b/leetcode/3601-3700/3625.Count-Number-of-Trapezoids-II/1.png differ diff --git a/leetcode/3601-3700/3625.Count-Number-of-Trapezoids-II/2.png b/leetcode/3601-3700/3625.Count-Number-of-Trapezoids-II/2.png new file mode 100644 index 00000000..c19b048b Binary files /dev/null and b/leetcode/3601-3700/3625.Count-Number-of-Trapezoids-II/2.png differ diff --git a/leetcode/3601-3700/3625.Count-Number-of-Trapezoids-II/README.md b/leetcode/3601-3700/3625.Count-Number-of-Trapezoids-II/README.md index 1f379be5..c05a9431 100755 --- a/leetcode/3601-3700/3625.Count-Number-of-Trapezoids-II/README.md +++ b/leetcode/3601-3700/3625.Count-Number-of-Trapezoids-II/README.md @@ -1,15 +1,41 @@ # [3625.Count Number of Trapezoids II][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. + +Return the number of unique trapezoids that can be formed by choosing any four distinct points from `points`. + +A **trapezoid** is a convex quadrilateral with **at least one pair** of parallel sides. Two lines are parallel if and only if they have the same slope. + +**Example 1:** + +![1](./1.png) + +``` +Input: points = [[-3,2],[3,0],[2,3],[3,2],[2,-3]] + +Output: 2 -**Example 1:** +Explanation: + +There are two distinct ways to pick four points that form a trapezoid: + +The points [-3,2], [2,3], [3,2], [2,-3] form one trapezoid. +The points [2,3], [3,2], [3,0], [2,-3] form another trapezoid. +``` + +**Example 2:** + +![2](./2.png) ``` -Input: a = "11", b = "1" -Output: "100" +Input: points = [[0,0],[1,0],[0,1],[2,1]] + +Output: 1 + +Explanation: + +There is only one trapezoid which can be formed. ``` ## 题意 diff --git a/leetcode/3601-3700/3625.Count-Number-of-Trapezoids-II/Solution.go b/leetcode/3601-3700/3625.Count-Number-of-Trapezoids-II/Solution.go index d115ccf5..be7f50ae 100644 --- a/leetcode/3601-3700/3625.Count-Number-of-Trapezoids-II/Solution.go +++ b/leetcode/3601-3700/3625.Count-Number-of-Trapezoids-II/Solution.go @@ -1,5 +1,67 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(points [][]int) int { + n := len(points) + inf := 1e9 + 7 + slopeToIntercept := make(map[float64][]float64) + midToSlope := make(map[float64][]float64) + ans := 0 + + for i := 0; i < n; i++ { + x1, y1 := points[i][0], points[i][1] + for j := i + 1; j < n; j++ { + x2, y2 := points[j][0], points[j][1] + dx := x1 - x2 + dy := y1 - y2 + + var k, b float64 + if x2 == x1 { + k = inf + b = float64(x1) + } else { + k = float64(y2-y1) / float64(x2-x1) + b = float64(y1*dx-x1*dy) / float64(dx) + } + + mid := float64((x1+x2)*10000 + (y1 + y2)) + slopeToIntercept[k] = append(slopeToIntercept[k], b) + midToSlope[mid] = append(midToSlope[mid], k) + } + } + + for _, sti := range slopeToIntercept { + if len(sti) == 1 { + continue + } + + cnt := make(map[float64]int) + for _, bVal := range sti { + cnt[bVal]++ + } + + totalSum := 0 + for _, count := range cnt { + ans += totalSum * count + totalSum += count + } + } + + for _, mts := range midToSlope { + if len(mts) == 1 { + continue + } + + cnt := make(map[float64]int) + for _, kVal := range mts { + cnt[kVal]++ + } + + totalSum := 0 + for _, count := range cnt { + ans -= totalSum * count + totalSum += count + } + } + + return ans } diff --git a/leetcode/3601-3700/3625.Count-Number-of-Trapezoids-II/Solution_test.go b/leetcode/3601-3700/3625.Count-Number-of-Trapezoids-II/Solution_test.go index 14ff50eb..605d7dff 100644 --- a/leetcode/3601-3700/3625.Count-Number-of-Trapezoids-II/Solution_test.go +++ b/leetcode/3601-3700/3625.Count-Number-of-Trapezoids-II/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{{-3, 2}, {3, 0}, {2, 3}, {3, 2}, {2, -3}}, 2}, + {"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() { }