Python Solution Based On hxtang's Idea

  • 2

    The basic idea is from: hxtang's Solution

    Python Code:

    class Solution(object):
        def isRectangleCover(self, rectangles):
            :type rectangles: List[List[int]]
            :rtype: bool
            left = min(x[0] for x in rectangles)
            bottom = min(x[1] for x in rectangles)
            right = max(x[2] for x in rectangles)
            top = max(x[3] for x in rectangles)
            points = collections.defaultdict(int)
            for l, b, r, t in rectangles:
                A, B, C, D = (l, b), (r, b), (r, t), (l, t)
                for p, q in zip((A, B, C, D), (1, 2, 4, 8)):
                    if points[p] & q: return False
                    points[p] |= q
            for px, py in points:
                if left < px < right or bottom < py < top:
                    if points[(px, py)] not in (3, 6, 9, 12, 15):
                        return False
            return True

    Further explanations:

  • 0

    I was going to post my solution, then I found yours cleaner. Thanks for sharing.
    Best python solution so far.

  • 0

    Clean code!..............

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.