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.
43 changes: 30 additions & 13 deletions leetcode/3601-3700/3623.Count-Number-of-Trapezoids-I/README.md
Original file line number Diff line number Diff line change
@@ -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.
```

## 结语

Expand Down
28 changes: 26 additions & 2 deletions leetcode/3601-3700/3623.Count-Number-of-Trapezoids-I/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
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{{1, 0}, {2, 0}, {3, 0}, {2, 2}, {3, 2}}, 3},
{"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