Skip to content

Commit d2e58b7

Browse files
committed
Add solution and test-cases for problem 3623
1 parent 7ce34f6 commit d2e58b7

File tree

5 files changed

+62
-22
lines changed

5 files changed

+62
-22
lines changed
43.3 KB
Loading
36.5 KB
Loading

leetcode/3601-3700/3623.Count-Number-of-Trapezoids-I/README.md

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
# [3623.Count Number of Trapezoids I][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a 2D integer array `points`, where `points[i] = [xi, yi]` represents the coordinates of the ith point on the Cartesian plane.
5+
6+
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.
7+
8+
Return the number of unique **horizontal trapezoids** that can be formed by choosing any four distinct points from `points`.
79

8-
**Example 1:**
10+
Since the answer may be very large, return it **modulo** `10^9 + 7`.
11+
12+
**Example 1:**
13+
14+
![1](./1.png)
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: points = [[1,0],[2,0],[3,0],[2,2],[3,2]]
18+
19+
Output: 3
20+
21+
Explanation:
22+
23+
There are three distinct ways to pick four points that form a horizontal trapezoid:
24+
25+
Using points [1,0], [2,0], [3,2], and [2,2].
26+
Using points [2,0], [3,0], [3,2], and [2,2].
27+
Using points [1,0], [3,0], [3,2], and [2,2].
1328
```
1429

15-
## 题意
16-
> ...
30+
**Example 2:**
1731

18-
## 题解
32+
![2](./2.png)
1933

20-
### 思路1
21-
> ...
22-
Count Number of Trapezoids I
23-
```go
2434
```
35+
Input: points = [[0,0],[1,0],[0,1],[2,1]]
2536
37+
Output: 1
38+
39+
Explanation:
40+
41+
There is only one horizontal trapezoid that can be formed.
42+
```
2643

2744
## 结语
2845

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
const mod = 1000000007
4+
5+
func Solution(points [][]int) int {
6+
columns := make(map[int]int)
7+
for _, p := range points {
8+
columns[p[1]]++
9+
}
10+
11+
pairs := []int{}
12+
sum, count := 0, 0
13+
for _, c := range columns {
14+
if c < 2 {
15+
continue
16+
}
17+
count = c * (c - 1) / 2
18+
sum = (sum + count) % mod
19+
pairs = append(pairs, count)
20+
}
21+
var ret int
22+
for _, pair := range pairs {
23+
ret = (ret + pair*((sum-pair+mod)%mod)) % mod
24+
}
25+
26+
// 使用模逆元乘以 1/2
27+
inv2 := (mod + 1) / 2
28+
return (ret * inv2) % mod
529
}

leetcode/3601-3700/3623.Count-Number-of-Trapezoids-I/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{{1, 0}, {2, 0}, {3, 0}, {2, 2}, {3, 2}}, 3},
17+
{"TestCase2", [][]int{{0, 0}, {1, 0}, {0, 1}, {2, 1}}, 1},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)