Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 32 additions & 6 deletions leetcode/3601-3700/3625.Count-Number-of-Trapezoids-II/README.md
Original file line number Diff line number Diff line change
@@ -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.
```

## 题意
Expand Down
66 changes: 64 additions & 2 deletions leetcode/3601-3700/3625.Count-Number-of-Trapezoids-II/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading