Skip to content
Merged
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
96 changes: 92 additions & 4 deletions solution/3600-3699/3623.Count Number of Trapezoids I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,120 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:枚举

根据题目描述,水平边满足 $y$ 坐标相同,因此我们可以根据 $y$ 坐标将点进行分组,统计每个 $y$ 坐标对应的点的数量。

我们用一个哈希表 $\textit{cnt}$ 来存储每个 $y$ 坐标对应的点的数量。对于每个 $y$ 坐标 $y_i$,假设对应的点的数量为 $v$,那么从这些点中选择两点作为水平边的方式有 $\binom{v}{2} = \frac{v(v-1)}{2}$ 种,记为 $t$。

我们用一个变量 $s$ 来记录之前所有 $y$ 坐标对应的水平边的数量之和。那么,我们可以将当前 $y$ 坐标对应的水平边的数量 $t$ 与之前所有 $y$ 坐标对应的水平边的数量之和 $s$ 相乘,得到以当前 $y$ 坐标为一对水平边的梯形的数量,并将其累加到答案中。最后,我们将当前 $y$ 坐标对应的水平边的数量 $t$ 累加到 $s$ 中,以便后续计算。

注意,由于答案可能非常大,我们需要对 $10^9 + 7$ 取余数。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是点的数量。

<!-- tabs:start -->

#### Python3

```python

class Solution:
def countTrapezoids(self, points: List[List[int]]) -> int:
mod = 10**9 + 7
cnt = Counter(p[1] for p in points)
ans = s = 0
for v in cnt.values():
t = v * (v - 1) // 2
ans = (ans + s * t) % mod
s += t
return ans
```

#### Java

```java

class Solution {
public int countTrapezoids(int[][] points) {
final int mod = (int) 1e9 + 7;
Map<Integer, Integer> cnt = new HashMap<>();
for (var p : points) {
cnt.merge(p[1], 1, Integer::sum);
}
long ans = 0, s = 0;
for (int v : cnt.values()) {
long t = 1L * v * (v - 1) / 2;
ans = (ans + s * t) % mod;
s += t;
}
return (int) ans;
}
}
```

#### C++

```cpp

class Solution {
public:
int countTrapezoids(vector<vector<int>>& points) {
const int mod = 1e9 + 7;
unordered_map<int, int> cnt;
for (auto& p : points) {
cnt[p[1]]++;
}
long long ans = 0, s = 0;
for (auto& [_, v] : cnt) {
long long t = 1LL * v * (v - 1) / 2;
ans = (ans + s * t) % mod;
s += t;
}
return (int) ans;
}
};
```

#### Go

```go
func countTrapezoids(points [][]int) int {
const mod = 1_000_000_007
cnt := make(map[int]int)
for _, p := range points {
cnt[p[1]]++
}

var ans, s int64
for _, v := range cnt {
t := int64(v) * int64(v-1) / 2
ans = (ans + s*t) % mod
s += t
}
return int(ans)
}
```

#### TypeScript

```ts
function countTrapezoids(points: number[][]): number {
const mod = 1_000_000_007;
const cnt = new Map<number, number>();

for (const p of points) {
cnt.set(p[1], (cnt.get(p[1]) ?? 0) + 1);
}

let ans = 0;
let s = 0;
for (const v of cnt.values()) {
const t = (v * (v - 1)) / 2;
const mul = BigInt(s) * BigInt(t);
ans = Number((BigInt(ans) + mul) % BigInt(mod));
s += t;
}

return ans;
}
```

<!-- tabs:end -->
Expand Down
96 changes: 92 additions & 4 deletions solution/3600-3699/3623.Count Number of Trapezoids I/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,32 +79,120 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Enumeration

According to the problem description, horizontal edges have the same $y$ coordinate. Therefore, we can group points by their $y$ coordinates and count the number of points for each $y$ coordinate.

We use a hash table $\textit{cnt}$ to store the number of points for each $y$ coordinate. For each $y$ coordinate $y_i$, assuming the number of corresponding points is $v$, the number of ways to select two points from these points as a horizontal edge is $\binom{v}{2} = \frac{v(v-1)}{2}$, denoted as $t$.

We use a variable $s$ to record the sum of the number of horizontal edges for all previous $y$ coordinates. Then, we can multiply the number of horizontal edges $t$ for the current $y$ coordinate by the sum $s$ of the number of horizontal edges for all previous $y$ coordinates to get the number of trapezoids with the current $y$ coordinate as one pair of horizontal edges, and add it to the answer. Finally, we add the number of horizontal edges $t$ for the current $y$ coordinate to $s$ for subsequent calculations.

Note that since the answer may be very large, we need to take the modulo $10^9 + 7$.

The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the number of points.

<!-- tabs:start -->

#### Python3

```python

class Solution:
def countTrapezoids(self, points: List[List[int]]) -> int:
mod = 10**9 + 7
cnt = Counter(p[1] for p in points)
ans = s = 0
for v in cnt.values():
t = v * (v - 1) // 2
ans = (ans + s * t) % mod
s += t
return ans
```

#### Java

```java

class Solution {
public int countTrapezoids(int[][] points) {
final int mod = (int) 1e9 + 7;
Map<Integer, Integer> cnt = new HashMap<>();
for (var p : points) {
cnt.merge(p[1], 1, Integer::sum);
}
long ans = 0, s = 0;
for (int v : cnt.values()) {
long t = 1L * v * (v - 1) / 2;
ans = (ans + s * t) % mod;
s += t;
}
return (int) ans;
}
}
```

#### C++

```cpp

class Solution {
public:
int countTrapezoids(vector<vector<int>>& points) {
const int mod = 1e9 + 7;
unordered_map<int, int> cnt;
for (auto& p : points) {
cnt[p[1]]++;
}
long long ans = 0, s = 0;
for (auto& [_, v] : cnt) {
long long t = 1LL * v * (v - 1) / 2;
ans = (ans + s * t) % mod;
s += t;
}
return (int) ans;
}
};
```

#### Go

```go
func countTrapezoids(points [][]int) int {
const mod = 1_000_000_007
cnt := make(map[int]int)
for _, p := range points {
cnt[p[1]]++
}

var ans, s int64
for _, v := range cnt {
t := int64(v) * int64(v-1) / 2
ans = (ans + s*t) % mod
s += t
}
return int(ans)
}
```

#### TypeScript

```ts
function countTrapezoids(points: number[][]): number {
const mod = 1_000_000_007;
const cnt = new Map<number, number>();

for (const p of points) {
cnt.set(p[1], (cnt.get(p[1]) ?? 0) + 1);
}

let ans = 0;
let s = 0;
for (const v of cnt.values()) {
const t = (v * (v - 1)) / 2;
const mul = BigInt(s) * BigInt(t);
ans = Number((BigInt(ans) + mul) % BigInt(mod));
s += t;
}

return ans;
}
```

<!-- tabs:end -->
Expand Down
17 changes: 17 additions & 0 deletions solution/3600-3699/3623.Count Number of Trapezoids I/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
int countTrapezoids(vector<vector<int>>& points) {
const int mod = 1e9 + 7;
unordered_map<int, int> cnt;
for (auto& p : points) {
cnt[p[1]]++;
}
long long ans = 0, s = 0;
for (auto& [_, v] : cnt) {
long long t = 1LL * v * (v - 1) / 2;
ans = (ans + s * t) % mod;
s += t;
}
return (int) ans;
}
};
15 changes: 15 additions & 0 deletions solution/3600-3699/3623.Count Number of Trapezoids I/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
func countTrapezoids(points [][]int) int {
const mod = 1_000_000_007
cnt := make(map[int]int)
for _, p := range points {
cnt[p[1]]++
}

var ans, s int64
for _, v := range cnt {
t := int64(v) * int64(v-1) / 2
ans = (ans + s*t) % mod
s += t
}
return int(ans)
}
16 changes: 16 additions & 0 deletions solution/3600-3699/3623.Count Number of Trapezoids I/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public int countTrapezoids(int[][] points) {
final int mod = (int) 1e9 + 7;
Map<Integer, Integer> cnt = new HashMap<>();
for (var p : points) {
cnt.merge(p[1], 1, Integer::sum);
}
long ans = 0, s = 0;
for (int v : cnt.values()) {
long t = 1L * v * (v - 1) / 2;
ans = (ans + s * t) % mod;
s += t;
}
return (int) ans;
}
}
10 changes: 10 additions & 0 deletions solution/3600-3699/3623.Count Number of Trapezoids I/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def countTrapezoids(self, points: List[List[int]]) -> int:
mod = 10**9 + 7
cnt = Counter(p[1] for p in points)
ans = s = 0
for v in cnt.values():
t = v * (v - 1) // 2
ans = (ans + s * t) % mod
s += t
return ans
19 changes: 19 additions & 0 deletions solution/3600-3699/3623.Count Number of Trapezoids I/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function countTrapezoids(points: number[][]): number {
const mod = 1_000_000_007;
const cnt = new Map<number, number>();

for (const p of points) {
cnt.set(p[1], (cnt.get(p[1]) ?? 0) + 1);
}

let ans = 0;
let s = 0;
for (const v of cnt.values()) {
const t = (v * (v - 1)) / 2;
const mul = BigInt(s) * BigInt(t);
ans = Number((BigInt(ans) + mul) % BigInt(mod));
s += t;
}

return ans;
}