-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday55
More file actions
55 lines (44 loc) · 1.47 KB
/
day55
File metadata and controls
55 lines (44 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#3625.count-number-of-trapezoids-ii:-
class Solution:
def countTrapezoids(self, points: List[List[int]]) -> int:
n = len(points)
inf = 10**9 + 7
slope_to_intercept = defaultdict(list)
mid_to_slope = defaultdict(list)
ans = 0
for i in range(n):
x1, y1 = points[i]
for j in range(i + 1, n):
x2, y2 = points[j]
dx = x1 - x2
dy = y1 - y2
if x2 == x1:
k = inf
b = x1
else:
k = (y2 - y1) / (x2 - x1)
b = (y1 * dx - x1 * dy) / dx
mid = (x1 + x2) * 10000 + (y1 + y2)
slope_to_intercept[k].append(b)
mid_to_slope[mid].append(k)
for sti in slope_to_intercept.values():
if len(sti) == 1:
continue
cnt = defaultdict(int)
for b_val in sti:
cnt[b_val] += 1
total_sum = 0
for count in cnt.values():
ans += total_sum * count
total_sum += count
for mts in mid_to_slope.values():
if len(mts) == 1:
continue
cnt = defaultdict(int)
for k_val in mts:
cnt[k_val] += 1
total_sum = 0
for count in cnt.values():
ans -= total_sum * count
total_sum += count
return ans