Skip to content

Commit 9b06416

Browse files
committed
feat: add solutions to lc problem: No.3623
1 parent 966f21c commit 9b06416

File tree

7 files changed

+261
-8
lines changed

7 files changed

+261
-8
lines changed

solution/3600-3699/3623.Count Number of Trapezoids I/README.md

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,32 +81,120 @@ tags:
8181

8282
<!-- solution:start -->
8383

84-
### 方法一
84+
### 方法一:枚举
85+
86+
根据题目描述,水平边满足 $y$ 坐标相同,因此我们可以根据 $y$ 坐标将点进行分组,统计每个 $y$ 坐标对应的点的数量。
87+
88+
我们用一个哈希表 $\textit{cnt}$ 来存储每个 $y$ 坐标对应的点的数量。对于每个 $y$ 坐标 $y_i$,假设对应的点的数量为 $v$,那么从这些点中选择两点作为水平边的方式有 $\binom{v}{2} = \frac{v(v-1)}{2}$ 种,记为 $t$。
89+
90+
我们用一个变量 $s$ 来记录之前所有 $y$ 坐标对应的水平边的数量之和。那么,我们可以将当前 $y$ 坐标对应的水平边的数量 $t$ 与之前所有 $y$ 坐标对应的水平边的数量之和 $s$ 相乘,得到以当前 $y$ 坐标为一对水平边的梯形的数量,并将其累加到答案中。最后,我们将当前 $y$ 坐标对应的水平边的数量 $t$ 累加到 $s$ 中,以便后续计算。
91+
92+
注意,由于答案可能非常大,我们需要对 $10^9 + 7$ 取余数。
93+
94+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是点的数量。
8595

8696
<!-- tabs:start -->
8797

8898
#### Python3
8999

90100
```python
91-
101+
class Solution:
102+
def countTrapezoids(self, points: List[List[int]]) -> int:
103+
mod = 10**9 + 7
104+
cnt = Counter(p[1] for p in points)
105+
ans = s = 0
106+
for v in cnt.values():
107+
t = v * (v - 1) // 2
108+
ans = (ans + s * t) % mod
109+
s += t
110+
return ans
92111
```
93112

94113
#### Java
95114

96115
```java
97-
116+
class Solution {
117+
public int countTrapezoids(int[][] points) {
118+
final int mod = (int) 1e9 + 7;
119+
Map<Integer, Integer> cnt = new HashMap<>();
120+
for (var p : points) {
121+
cnt.merge(p[1], 1, Integer::sum);
122+
}
123+
long ans = 0, s = 0;
124+
for (int v : cnt.values()) {
125+
long t = 1L * v * (v - 1) / 2;
126+
ans = (ans + s * t) % mod;
127+
s += t;
128+
}
129+
return (int) ans;
130+
}
131+
}
98132
```
99133

100134
#### C++
101135

102136
```cpp
103-
137+
class Solution {
138+
public:
139+
int countTrapezoids(vector<vector<int>>& points) {
140+
const int mod = 1e9 + 7;
141+
unordered_map<int, int> cnt;
142+
for (auto& p : points) {
143+
cnt[p[1]]++;
144+
}
145+
long long ans = 0, s = 0;
146+
for (auto& [_, v] : cnt) {
147+
long long t = 1LL * v * (v - 1) / 2;
148+
ans = (ans + s * t) % mod;
149+
s += t;
150+
}
151+
return (int) ans;
152+
}
153+
};
104154
```
105155
106156
#### Go
107157
108158
```go
159+
func countTrapezoids(points [][]int) int {
160+
const mod = 1_000_000_007
161+
cnt := make(map[int]int)
162+
for _, p := range points {
163+
cnt[p[1]]++
164+
}
165+
166+
var ans, s int64
167+
for _, v := range cnt {
168+
t := int64(v) * int64(v-1) / 2
169+
ans = (ans + s*t) % mod
170+
s += t
171+
}
172+
return int(ans)
173+
}
174+
```
175+
176+
#### TypeScript
177+
178+
```ts
179+
function countTrapezoids(points: number[][]): number {
180+
const mod = 1_000_000_007;
181+
const cnt = new Map<number, number>();
182+
183+
for (const p of points) {
184+
cnt.set(p[1], (cnt.get(p[1]) ?? 0) + 1);
185+
}
186+
187+
let ans = 0;
188+
let s = 0;
189+
for (const v of cnt.values()) {
190+
const t = (v * (v - 1)) / 2;
191+
const mul = BigInt(s) * BigInt(t);
192+
ans = Number((BigInt(ans) + mul) % BigInt(mod));
193+
s += t;
194+
}
109195

196+
return ans;
197+
}
110198
```
111199

112200
<!-- tabs:end -->

solution/3600-3699/3623.Count Number of Trapezoids I/README_EN.md

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,32 +79,120 @@ tags:
7979

8080
<!-- solution:start -->
8181

82-
### Solution 1
82+
### Solution 1: Enumeration
83+
84+
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.
85+
86+
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$.
87+
88+
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.
89+
90+
Note that since the answer may be very large, we need to take the modulo $10^9 + 7$.
91+
92+
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the number of points.
8393

8494
<!-- tabs:start -->
8595

8696
#### Python3
8797

8898
```python
89-
99+
class Solution:
100+
def countTrapezoids(self, points: List[List[int]]) -> int:
101+
mod = 10**9 + 7
102+
cnt = Counter(p[1] for p in points)
103+
ans = s = 0
104+
for v in cnt.values():
105+
t = v * (v - 1) // 2
106+
ans = (ans + s * t) % mod
107+
s += t
108+
return ans
90109
```
91110

92111
#### Java
93112

94113
```java
95-
114+
class Solution {
115+
public int countTrapezoids(int[][] points) {
116+
final int mod = (int) 1e9 + 7;
117+
Map<Integer, Integer> cnt = new HashMap<>();
118+
for (var p : points) {
119+
cnt.merge(p[1], 1, Integer::sum);
120+
}
121+
long ans = 0, s = 0;
122+
for (int v : cnt.values()) {
123+
long t = 1L * v * (v - 1) / 2;
124+
ans = (ans + s * t) % mod;
125+
s += t;
126+
}
127+
return (int) ans;
128+
}
129+
}
96130
```
97131

98132
#### C++
99133

100134
```cpp
101-
135+
class Solution {
136+
public:
137+
int countTrapezoids(vector<vector<int>>& points) {
138+
const int mod = 1e9 + 7;
139+
unordered_map<int, int> cnt;
140+
for (auto& p : points) {
141+
cnt[p[1]]++;
142+
}
143+
long long ans = 0, s = 0;
144+
for (auto& [_, v] : cnt) {
145+
long long t = 1LL * v * (v - 1) / 2;
146+
ans = (ans + s * t) % mod;
147+
s += t;
148+
}
149+
return (int) ans;
150+
}
151+
};
102152
```
103153
104154
#### Go
105155
106156
```go
157+
func countTrapezoids(points [][]int) int {
158+
const mod = 1_000_000_007
159+
cnt := make(map[int]int)
160+
for _, p := range points {
161+
cnt[p[1]]++
162+
}
163+
164+
var ans, s int64
165+
for _, v := range cnt {
166+
t := int64(v) * int64(v-1) / 2
167+
ans = (ans + s*t) % mod
168+
s += t
169+
}
170+
return int(ans)
171+
}
172+
```
173+
174+
#### TypeScript
175+
176+
```ts
177+
function countTrapezoids(points: number[][]): number {
178+
const mod = 1_000_000_007;
179+
const cnt = new Map<number, number>();
180+
181+
for (const p of points) {
182+
cnt.set(p[1], (cnt.get(p[1]) ?? 0) + 1);
183+
}
184+
185+
let ans = 0;
186+
let s = 0;
187+
for (const v of cnt.values()) {
188+
const t = (v * (v - 1)) / 2;
189+
const mul = BigInt(s) * BigInt(t);
190+
ans = Number((BigInt(ans) + mul) % BigInt(mod));
191+
s += t;
192+
}
107193

194+
return ans;
195+
}
108196
```
109197

110198
<!-- tabs:end -->
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int countTrapezoids(vector<vector<int>>& points) {
4+
const int mod = 1e9 + 7;
5+
unordered_map<int, int> cnt;
6+
for (auto& p : points) {
7+
cnt[p[1]]++;
8+
}
9+
long long ans = 0, s = 0;
10+
for (auto& [_, v] : cnt) {
11+
long long t = 1LL * v * (v - 1) / 2;
12+
ans = (ans + s * t) % mod;
13+
s += t;
14+
}
15+
return (int) ans;
16+
}
17+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func countTrapezoids(points [][]int) int {
2+
const mod = 1_000_000_007
3+
cnt := make(map[int]int)
4+
for _, p := range points {
5+
cnt[p[1]]++
6+
}
7+
8+
var ans, s int64
9+
for _, v := range cnt {
10+
t := int64(v) * int64(v-1) / 2
11+
ans = (ans + s*t) % mod
12+
s += t
13+
}
14+
return int(ans)
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int countTrapezoids(int[][] points) {
3+
final int mod = (int) 1e9 + 7;
4+
Map<Integer, Integer> cnt = new HashMap<>();
5+
for (var p : points) {
6+
cnt.merge(p[1], 1, Integer::sum);
7+
}
8+
long ans = 0, s = 0;
9+
for (int v : cnt.values()) {
10+
long t = 1L * v * (v - 1) / 2;
11+
ans = (ans + s * t) % mod;
12+
s += t;
13+
}
14+
return (int) ans;
15+
}
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def countTrapezoids(self, points: List[List[int]]) -> int:
3+
mod = 10**9 + 7
4+
cnt = Counter(p[1] for p in points)
5+
ans = s = 0
6+
for v in cnt.values():
7+
t = v * (v - 1) // 2
8+
ans = (ans + s * t) % mod
9+
s += t
10+
return ans
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function countTrapezoids(points: number[][]): number {
2+
const mod = 1_000_000_007;
3+
const cnt = new Map<number, number>();
4+
5+
for (const p of points) {
6+
cnt.set(p[1], (cnt.get(p[1]) ?? 0) + 1);
7+
}
8+
9+
let ans = 0;
10+
let s = 0;
11+
for (const v of cnt.values()) {
12+
const t = (v * (v - 1)) / 2;
13+
const mul = BigInt(s) * BigInt(t);
14+
ans = Number((BigInt(ans) + mul) % BigInt(mod));
15+
s += t;
16+
}
17+
18+
return ans;
19+
}

0 commit comments

Comments
 (0)