Minimum Area Rectangle LeetCode solution in Python
1class Solution:2def minAreaRect(self, points: List[List[int]]) -> int:3points = sorted(points, key=lambda item: (item[0], item[1]))4cols = defaultdict(list)56for x, y in points:7cols[x].append(y)89lastx = {}10ans = float("inf")1112for x in cols:13col = cols[x]14for i, y1 in enumerate(col):15for j in range(i):16y2 = col[j]17if (y2, y1) in lastx:18ans = min(ans, abs((x - lastx[y2, y1]) * (y2 - y1)))19lastx[y2, y1] = x2021return 0 if ans == float("inf") else ans