Minimum Area Rectangle LeetCode solution in Python

1
class Solution:
2
def minAreaRect(self, points: List[List[int]]) -> int:
3
points = sorted(points, key=lambda item: (item[0], item[1]))
4
cols = defaultdict(list)
5

6
for x, y in points:
7
cols[x].append(y)
8

9
lastx = {}
10
ans = float("inf")
11

12
for x in cols:
13
col = cols[x]
14
for i, y1 in enumerate(col):
15
for j in range(i):
16
y2 = col[j]
17
if (y2, y1) in lastx:
18
ans = min(ans, abs((x - lastx[y2, y1]) * (y2 - y1)))
19
lastx[y2, y1] = x
20

21
return 0 if ans == float("inf") else ans

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0