Python solution, easy to read

  • 1
    class Solution(object):
        def computeArea(self, A, B, C, D, E, F, G, H):
            :type A: int
            :type B: int
            :type C: int
            :type D: int
            :type E: int
            :type F: int
            :type G: int
            :type H: int
            :rtype: int
            # Calculate overlap points
            leftx = max(A,E)
            lefty = max(B,F)
            rightx = min(C,G)
            righty = min(D,H)
            plusArea = (C-A)*(D-B) + (G-E)*(H-F)
            ## If the low left corner is greater than the upper right corner, then there is no overlap.
            if leftx > rightx or lefty > righty: return plusArea
            length = rightx - leftx
            width = righty - lefty
            return plusArea - length*width

Log in to reply

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