-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0939-minimum-area-rectangle.py
More file actions
28 lines (21 loc) · 904 Bytes
/
0939-minimum-area-rectangle.py
File metadata and controls
28 lines (21 loc) · 904 Bytes
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
# time complexity: O(n^2)
# space complexity: O(n)
from typing import List
class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
hashMap = {}
for singlePoint in points:
if singlePoint[0] not in hashMap:
hashMap[singlePoint[0]] = set()
hashMap[singlePoint[0]].add(singlePoint[1])
miniArea = float("inf")
for i in range(len(points)):
for j in range(len(points)):
x1, y1 = points[i][0], points[i][1]
x2, y2 = points[j][0], points[j][1]
if x1 != x2 and y1 != y2:
if y2 in hashMap[x1] and y1 in hashMap[x2]:
miniArea = min(miniArea, abs(x1-x2) * abs(y1-y2))
return miniArea if miniArea != float('inf') else 0
points = [[1, 1], [1, 3], [3, 1], [3, 3], [2, 2]]
print(Solution().minAreaRect(points))