Leetcode 976 Solution

This article provides solution to leetcode question 976 (minimum-area-rectangle)

https://leetcode.com/problems/minimum-area-rectangle

Solution

class Solution(object):
    def minAreaRect(self, points):
        """
        :type points: List[List[int]]
        :rtype: int
        """
        s = {(pt[0], pt[1]) for pt in points}

        ans = sys.maxsize

        for pt1 in points:
            for pt2 in points:
                if not (pt1[0] < pt2[0] and pt1[1] < pt2[1]):
                    continue

                if (pt1[0], pt2[1]) not in s:
                    continue

                if (pt2[0], pt1[1]) not in s:
                    continue

                ans = min(ans, (pt2[0] - pt1[0]) * (pt2[1] - pt1[1]))

        return ans if ans != sys.maxsize else 0