Skip to content

Commit ad6edf0

Browse files
committed
Time: 91 ms (29.84%), Space: 67.6 MB (10.41%) - LeetHub
1 parent 51b32ec commit ad6edf0

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# time complexity: O(n)
2+
# space complexity: O(n)
3+
from collections import defaultdict
4+
from typing import List
5+
from math import comb
6+
7+
MOD = 10**9 + 7
8+
9+
10+
class Solution:
11+
def countTrapezoids(self, points: List[List[int]]) -> int:
12+
yPoints = defaultdict(list)
13+
for x, y in points:
14+
yPoints[y].append(x)
15+
16+
pairsPerY = []
17+
for y in yPoints:
18+
m = len(yPoints[y])
19+
if m >= 2:
20+
count = comb(m, 2)
21+
pairsPerY.append(count)
22+
23+
sumPairs = sum(pairsPerY)
24+
sumSquares = sum(count * count for count in pairsPerY)
25+
total = (sumPairs * sumPairs - sumSquares) // 2
26+
27+
return total % MOD
28+
29+
30+
points = [[1, 0], [2, 0], [3, 0], [2, 2], [3, 2]]
31+
print(Solution().countTrapezoids(points))
32+
points = [[0, 0], [1, 0], [0, 1], [2, 1]]
33+
print(Solution().countTrapezoids(points))

0 commit comments

Comments
 (0)