-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
44 lines (33 loc) · 831 Bytes
/
solver.py
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
#
# Time complexity: O(n^2)
# Space complexity: O(n/2) (worst case, where we've distinct vertical lines)
#
def count_rectangles(points):
vertical_lines = {}
for p1 in points:
for p2 in points:
# Same points don't make a line
if p1 == p2:
continue
if stacked(p1, p2):
point = make_point(p1, p2)
vertical_lines[point] = vertical_lines.get(point, 0) + 1
acc = 0
for count in vertical_lines.values():
# We need at least two vertical lines to make a rectangle
if count < 2:
continue
acc += 1 if count == 2 else count
return acc
def make_point(p1, p2):
p1_y, p2_y = p1[1], p2[1]
return (
min(p1_y, p2_y),
max(p1_y, p2_y)
)
def stacked(p1, p2):
p1_x, p1_y = p1
p2_x, p2_y = p2
if p1_x != p2_x:
return False
return p1_y < p2_y