-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathrectangle-mania.py
162 lines (142 loc) · 4.13 KB
/
rectangle-mania.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# RECTANGLE MANIA
# O(N^2) time and space
def rectangleMania(coords):
# Write your code here.
coordsTable = getCoordsTable(coords)
return getRectangleCount(coords, coordsTable)
def getCoordsTable(coords):
coordsTable = {}
for coord1 in coords:
coord1Directions = {UP: [], RIGHT: [], DOWN: [], LEFT: []}
for coord2 in coords:
coord2Direction = getCoordDirection(coord1, coord2)
if coord2Direction in coord1Directions:
coord1Directions[coord2Direction].append(coord2)
coord1String = coordToString(coord1)
coordsTable[coord1String] = coord1Directions
return coordsTable
def getCoordDirection(coord1, coord2):
x1, y1 = coord1
x2, y2 = coord2
if y2 == y1:
if x2 > x1:
return RIGHT
elif x2 < x1:
return LEFT
elif x2 == x1:
if y2 > y1:
return UP
elif y2 < y1:
return DOWN
return ""
def getRectangleCount(coords, coordsTable):
rectangleCount = 0
for coord in coords:
rectangleCount += clockwiseCountRectangles(coord, coordsTable, UP, coord)
return rectangleCount
def clockwiseCountRectangles(coord, coordsTable, direction, origin):
coordString = coordToString(coord)
if direction == LEFT:
rectangleFound = origin in coordsTable[coordString][LEFT]
return 1 if rectangleFound else 0
else:
rectangleCount = 0
nextDirection = getNextClockwiseDirection(direction)
for nextCoord in coordsTable[coordString][direction]:
rectangleCount += clockwiseCountRectangles(nextCoord, coordsTable, nextDirection, origin)
return rectangleCount
def getNextClockwiseDirection(direction):
if direction == UP:
return RIGHT
if direction == RIGHT:
return DOWN
if direction == DOWN:
return LEFT
return ""
def coordToString(coord):
x, y = coord
return str(x) + "-" + str(y)
UP = "up"
RIGHT = "right"
DOWN = "down"
LEFT = "left"
# O(N^2) time and O(N) space
def rectangleMania(coords):
# Write your code here.
coordsTable = getCoordsTable(coords)
return getRectangleCount(coords, coordsTable)
def getCoordsTable(coords):
coordsTable = {"x": {}, "y": {}}
for coord in coords:
x, y = coord
if x not in coordsTable["x"]:
coordsTable["x"][x] = []
coordsTable["x"][x].append(coord)
if y not in coordsTable["y"]:
coordsTable["y"][y] = []
coordsTable["y"][y].append(coord)
return coordsTable
def getRectangleCount(coords, coordsTable):
rectangleCount = 0
for coord in coords:
lowerLeftY = coord[1]
rectangleCount += clockwiseCountRectangles(coord, coordsTable, UP, lowerLeftY)
return rectangleCount
def clockwiseCountRectangles(coord1, coordsTable, direction, lowerLeftY):
x1, y1 = coord1
if direction == DOWN:
relevantCoords = coordsTable["x"][x1]
for coord2 in relevantCoords:
lowerRightY = coord2[1]
if lowerRightY == lowerLeftY:
return 1
return 0
else:
rectangleCount = 0
if direction == UP:
relevantCoords = coordsTable["x"][x1]
for coord2 in relevantCoords:
y2 = coord2[1]
isAbove = y2 > y1
if isAbove:
rectangleCount += clockwiseCountRectangles(coord2, coordsTable, RIGHT, lowerLeftY)
elif direction == RIGHT:
relevantCoords = coordsTable["y"][y1]
for coord2 in relevantCoords:
x2 = coord2[0]
isRight = x2 > x1
if isRight:
rectangleCount += clockwiseCountRectangles(coord2, coordsTable, DOWN, lowerLeftY)
return rectangleCount
UP = "up"
RIGHT = "right"
DOWN = "down"
# O(N^2) time and O(N) space
def rectangleMania(coords):
# Write your code here.
coordsTable = getCoordsTable(coords)
return getRectangleCount(coords, coordsTable)
def getCoordsTable(coords):
coordsTable = {}
for coord in coords:
coordString = coordToString(coord)
coordsTable[coordString] = True
return coordsTable
def getRectangleCount(coords, coordsTable):
rectangleCount = 0
for x1, y1 in coords:
for x2, y2 in coords:
if not isInUpperRight([x1, y1], [x2, y2]):
continue
upperCoordString = coordToString([x1, y2])
rightCoordString = coordToString([x2, y1])
if upperCoordString in coordsTable and rightCoordString in coordsTable:
rectangleCount += 1
return rectangleCount
def isInUpperRight(coord1, coord2):
x1, y1 = coord1
x2, y2 = coord2
return x2 > x1 and y2 > y1
def coordToString(coord):
x, y = coord
return str(x) + "-" + str(y)