Skip to content

Commit 8ed2257

Browse files
committed
Time: 1269 ms (98.8%), Space: 68.6 MB (98.97%) - LeetHub
1 parent dee0c7b commit 8ed2257

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# time complexity: O(n^2)
2+
# space complexity: O(n^2)
3+
from collections import defaultdict
4+
from typing import List
5+
6+
7+
class Solution:
8+
def countTrapezoids(self, points: List[List[int]]) -> int:
9+
n = len(points)
10+
inf = 10**9 + 7
11+
slopeToIntercept = defaultdict(list)
12+
midToSlope = defaultdict(list)
13+
result = 0
14+
15+
for i in range(n):
16+
x1, y1 = points[i]
17+
for j in range(i + 1, n):
18+
x2, y2 = points[j]
19+
dx = x1 - x2
20+
dy = y1 - y2
21+
22+
if x2 == x1:
23+
k = inf
24+
b = x1
25+
else:
26+
k = (y2 - y1) / (x2 - x1)
27+
b = (y1 * dx - x1 * dy) / dx
28+
29+
mid = (x1 + x2) * 10000 + (y1 + y2)
30+
slopeToIntercept[k].append(b)
31+
midToSlope[mid].append(k)
32+
33+
for sti in slopeToIntercept.values():
34+
if len(sti) == 1:
35+
continue
36+
37+
counter = defaultdict(int)
38+
for bVal in sti:
39+
counter[bVal] += 1
40+
41+
totalSum = 0
42+
for count in counter.values():
43+
result += totalSum * count
44+
totalSum += count
45+
46+
for mts in midToSlope.values():
47+
if len(mts) == 1:
48+
continue
49+
50+
counter = defaultdict(int)
51+
for kVal in mts:
52+
counter[kVal] += 1
53+
54+
totalSum = 0
55+
for count in counter.values():
56+
result -= totalSum * count
57+
totalSum += count
58+
59+
return result
60+
61+
62+
points = [[-3, 2], [3, 0], [2, 3], [3, 2], [2, -3]]
63+
print(Solution().countTrapezoids(points))
64+
points = [[0, 0], [1, 0], [0, 1], [2, 1]]
65+
print(Solution().countTrapezoids(points))

0 commit comments

Comments
 (0)