share my python solution

  • 1
    class Solution(object):
        def computeArea(self, A, B, C, D, E, F, G, H):
            l1 = [A, C, E, G] # x axis
            l2 = [B, D, F, H] # y axis
            # remove the deplicate part
            return self.area(A,B,C,D) +self.area(E,F,G,H)- self.getCoveredLength(l1) * self.getCoveredLength(l2)
        def area(self, A, B, C, D):
            # calculate the area of rectangle ABCD
            return abs(C-A) * abs(D-B)
        def getCoveredLength(self, l):
            # get the intersection part of two rectangles
            sum_len = abs(l[0]-l[1])+abs(l[2]-l[3])
            max_len = abs(l[0] - l[-1])
            if sum_len < max_len:
                # which means no intersection
                return 0
            return sum_len - max_len

Log in to reply

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